在JavaScript中,当从浮点数转换为字符串时,如何才能在小数点后得到2位数字?例如,0.34而不是0.3445434。


当前回答

countDecimals = value => { if (Math.floor(value) === value) return 0; let stringValue = value.toString().split(".")[1]; if (stringValue) { return value.toString().split(".")[1].length ? value.toString().split(".")[1].length : 0; } else { return 0; } }; formatNumber=(ans)=>{ let decimalPlaces = this.countDecimals(ans); ans = 1 * ans; if (decimalPlaces !== 0) { let onePlusAns = ans + 1; let decimalOnePlus = this.countDecimals(onePlusAns); if (decimalOnePlus < decimalPlaces) { ans = ans.toFixed(decimalPlaces - 1).replace(/\.?0*$/, ""); } else { let tenMulAns = ans * 10; let decimalTenMul = this.countDecimals(tenMulAns); if (decimalTenMul + 1 < decimalPlaces) { ans = ans.toFixed(decimalPlaces - 1).replace(/\.?0*$/, ""); } } } }

我只是给这个值加上1,然后数出原始值和增加的值中出现的十进制数字。如果我在比原始十进制数字少加1之后找到十进制数字,我只调用toFixed() with(原始小数- 1)。我还通过将原始值乘以10来检查,并遵循相同的逻辑,以防加1不会减少冗余小数位。 在JS中处理浮点数舍入的简单变通方法。我试过的大多数情况下都有效。

其他回答

另一个需要注意的问题是,toFixed()会在数字末尾产生不必要的零。 例如:

var x=(23-7.37)
x
15.629999999999999
x.toFixed(6)
"15.630000"

这个想法是使用RegExp清理输出:

function humanize(x){
  return x.toFixed(6).replace(/\.?0*$/,'');
}

RegExp匹配后面的零(可选的还有小数点),以确保它也适合整数。

humanize(23-7.37)
"15.63"
humanize(1200)
"1200"
humanize(1200.03)
"1200.03"
humanize(3/4)
"0.75"
humanize(4/3)
"1.333333"
function trimNumber(num, len) {
  const modulu_one = 1;
  const start_numbers_float=2;
  var int_part = Math.trunc(num);
  var float_part = String(num % modulu_one);
      float_part = float_part.slice(start_numbers_float, start_numbers_float+len);
  return int_part+'.'+float_part;
}

There is no way to avoid inconsistent rounding for prices with x.xx5 as actual value using either multiplication or division. If you need to calculate correct prices client-side you should keep all amounts in cents. This is due to the nature of the internal representation of numeric values in JavaScript. Notice that Excel suffers from the same problems so most people wouldn't notice the small errors caused by this phenomen. However errors may accumulate whenever you add up a lot of calculated values, there is a whole theory around this involving the order of calculations and other methods to minimize the error in the final result. To emphasize on the problems with decimal values, please note that 0.1 + 0.2 is not exactly equal to 0.3 in JavaScript, while 1 + 2 is equal to 3.

/** don't spend 5 minutes, use my code **/
function prettyFloat(x,nbDec) { 
    if (!nbDec) nbDec = 100;
    var a = Math.abs(x);
    var e = Math.floor(a);
    var d = Math.round((a-e)*nbDec); if (d == nbDec) { d=0; e++; }
    var signStr = (x<0) ? "-" : " ";
    var decStr = d.toString(); var tmp = 10; while(tmp<nbDec && d*tmp < nbDec) {decStr = "0"+decStr; tmp*=10;}
    var eStr = e.toString();
    return signStr+eStr+"."+decStr;
}

prettyFloat(0);      //  "0.00"
prettyFloat(-1);     // "-1.00"
prettyFloat(-0.999); // "-1.00"
prettyFloat(0.5);    //  "0.50"

countDecimals = value => { if (Math.floor(value) === value) return 0; let stringValue = value.toString().split(".")[1]; if (stringValue) { return value.toString().split(".")[1].length ? value.toString().split(".")[1].length : 0; } else { return 0; } }; formatNumber=(ans)=>{ let decimalPlaces = this.countDecimals(ans); ans = 1 * ans; if (decimalPlaces !== 0) { let onePlusAns = ans + 1; let decimalOnePlus = this.countDecimals(onePlusAns); if (decimalOnePlus < decimalPlaces) { ans = ans.toFixed(decimalPlaces - 1).replace(/\.?0*$/, ""); } else { let tenMulAns = ans * 10; let decimalTenMul = this.countDecimals(tenMulAns); if (decimalTenMul + 1 < decimalPlaces) { ans = ans.toFixed(decimalPlaces - 1).replace(/\.?0*$/, ""); } } } }

我只是给这个值加上1,然后数出原始值和增加的值中出现的十进制数字。如果我在比原始十进制数字少加1之后找到十进制数字,我只调用toFixed() with(原始小数- 1)。我还通过将原始值乘以10来检查,并遵循相同的逻辑,以防加1不会减少冗余小数位。 在JS中处理浮点数舍入的简单变通方法。我试过的大多数情况下都有效。