JavaScript使用IEEE 标准进行二进制浮点运算产生莫名错误的解决方法_javascript技巧

WBOY
Release: 2016-05-16 18:06:09
Original
968 people have browsed it

javascript做带小数的计算时,会出现9的循环,以下方法帮助解决。

第一个参数是需要处理的数值,第二个参数是需要保留的小数位数。自家项目里面就这么用了~

复制代码代码如下:

function round(v,e){
var t=1;
for(;e>0;t*=10,e--);
for(;ereturn Math.round(v*t)/t;
}
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!