javascript - how to prevent toFixed from rounding off decimal numbers

前端 未结 5 1444
栀梦
栀梦 2020-11-28 14:29

I\'m very new to html, javascript, and css so please forgive if my question sounds idiotic to you. My question is how can I prevent the function toFixed() from

5条回答
  •  温柔的废话
    2020-11-28 15:12

    you can give this a try, it won't round your decimals

    /**
     * @param {any} input 
     * @param {number} decimals 
     */
    var toFixed = function(input, decimals) {
      var arr = ("" + input).split(".");
      if (arr.length === 1) return input;
      var int = arr[0],
          max = arr[1].length,
          dec = arr[1].substr(0, decimals > max ? max : decimals);
      return decimals === 0 ? int : [int, "." , dec].join("");
    }
    

提交回复
热议问题