JS rounding problem of special data
2.885 |
7131.0 |
两数相乘:20572.934999999998 |
四舍五入后结果是:20572.93 |
What’s going on?
]
A better solution:
If you need to introduce external Js, you need to refresh to execute
]<script>
var s = 2, n = 20572.934999999998;
var l = n.toString().length - (n.toString().indexOf('.') + 1);
for (var i = l; i >= s; i --) { n = new Number(n.toFixed(i)); }
alert(n);
</script>