在使用javascript舍入值时,我遇到了奇怪的问题。某些值不是以正确的格式舍入。
var n =17.955 ;
var roundedPrice;
roundedPrice = Math.round(n*100)/100;
console.log(roundedPrice); // It returns 17.95 instead of 17.96
正在发生某些特定值,例如16.955, 17.955, 18.955, 19.955
。除了这些值,如1.955, 12.955, 20.955, 27.955 ...
此圆函数返回正确的值。
编辑:仅发生在17.955。这将返回17.9555(3次5)的正确结果。
提前致谢。
答案 0 :(得分:-3)
您可以使用Math.ceil()
获得预期结果。