JS 四舍五入保留两位小数

var num =2.446242342;
num = num.toFixed(2);  // 输出结果为 2.45

另外像 round()、floor()、ceil() 等都不能真正的四舍五入,有精度问题。

round() 可以通过以下方式来确保精度是正确的:

var num =2.446242342;
num = Math.round((num + Number.EPSILON) * 100) / 100;  // 输出结果为 2.45

  

猜你喜欢

转载自www.cnblogs.com/diehuacanmeng/p/13403178.html
今日推荐