在这个例子中/行为是非常奇怪的。
为什么函数toFixed
前两个例子有效,而最后一个例子无效?
//example 1
var num = 554.956;
var n = num.toFixed(2)
console.log(n);
var num2 = 554.955;
var n2 = num2.toFixed(2)
console.log(n2);
//output 554.96 and 554.96
//example 2
var num5 = 5.956;
var n5 = num5.toFixed(2)
console.log(n5);
var num6 = 5.955;
var n6 = num6.toFixed(2)
console.log(n6);
//output 5.96 and 5.96
//example 3
var num3 = 55.956;
var n3 = num3.toFixed(2)
console.log(n3);
var num4 = 55.955;
var n4 = num4.toFixed(2)
console.log(n4);
//output 55.96 and 55.95
相关:php round 与 javascript toFixed https://stackoverflow.com/questions/43802830/php-roundnum-2-and-javascipt-tofixed2-is-not-giving-right-output-for-this-v?noredirect=1#comment74645553_43802830
编辑:关于重复项,尤其是这个:toFixed 功能无法正常工作(请给出原因而不是替代方案) https://stackoverflow.com/questions/29008083/tofixed-function-not-working-properly-please-give-a-reason-not-an-alternative
答案非常好,帮助我理解 x.955 和 x.956 之间的区别,但没有回答为什么这种情况只发生在55.955
在我的例子中,而不是5.955
or 554.955
.
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)