Home  >  Article  >  Web Front-end  >  js rounding problem of special data_basic knowledge

js rounding problem of special data_basic knowledge

WBOY
WBOYOriginal
2016-05-16 19:18:20931browse
JS rounding problem of special data

2.885 7131.0 两数相乘:20572.934999999998 四舍五入后结果是:20572.93
What’s going on?

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

A better solution:

[Ctrl A select all Note:
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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn