详解js加减乘除精确计算
作者:thelongmarch 时间:2024-04-18 10:47:10
JS无法进行精确计算的bug
在做CRM,二代审核需求审核详情页面时。需要按比例(后端传类似0.8的小数)把用户输入的数字显示在不同的地方。
在做dubheInvest = invest * (1 - ratio);运算时发现问题。具体如下:
示例代码:
console.log( 1 - 0.8 ); //输出 0.19999999999999996
console.log( 6 * 0.7 ); //输出 4.199999999999999
console.log( 0.1 + 0.2 ); //输出 0.30000000000000004
console.log( 0.1 + 0.7 ); //输出 0.7999999999999999
console.log( 1.2 / 0.2 ); //输出 5.999999999999999
通过上面举出的例子可以看到,原生的js运算结果不一定准确,会丢失精度。
解决方案
解决方案的原理是,将浮点数乘以(扩大)10的n次方倍,把浮点数变为整数后再进行相应的运算,最后将得到的结果除以(缩小)10的n次方倍。
原理示例:
将console.log(1-0.8); 变为 console.log((1 * 10 - 0.8 * 10) / 10); 即可得到正确的值
根据上述原理,可以封装一些方法出来解决此类问题。如下所示(Math.pow(x, y);表示求x的y次方):
//加
function floatAdd(arg1,arg2){
var r1,r2,m;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
return (arg1*m+arg2*m)/m;
}
//减
function floatSub(arg1,arg2){
var r1,r2,m,n;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
//动态控制精度长度
n=(r1>=r2)?r1:r2;
return ((arg1*m-arg2*m)/m).toFixed(n);
}
//乘
function floatMul(arg1,arg2) {
var m=0,s1=arg1.toString(),s2=arg2.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}
//除
function floatDiv(arg1,arg2){
var t1=0,t2=0,r1,r2;
try{t1=arg1.toString().split(".")[1].length}catch(e){}
try{t2=arg2.toString().split(".")[1].length}catch(e){}
r1=Number(arg1.toString().replace(".",""));
r2=Number(arg2.toString().replace(".",""));
return (r1/r2)*Math.pow(10,t2-t1);
}
以上所述是小编给大家介绍的js加减乘除精确计算详解整合网站的支持!
来源:https://www.cnblogs.com/thelongmarch/p/9321748.html
标签:js,加减乘除
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PythonWeb项目Django部署在Ubuntu18.04腾讯云主机上
2021-02-11 11:15:33
![](https://img.aspxhome.com/file/2023/1/77921_0s.png)
Python高级文件操作之shutil库详解
2022-05-28 10:23:42
![](https://img.aspxhome.com/file/2023/6/110076_0s.png)
python实现简单图片物体标注工具
2021-09-07 21:31:50
![](https://img.aspxhome.com/file/2023/9/135209_0s.jpg)
2个asp获取安全字符串的方法
2007-10-19 15:08:00
使用Golang的singleflight防止缓存击穿的方法
2024-05-22 10:12:29
对于Python装饰器使用的一些建议
2022-05-26 09:05:43
PHP Session变量不能传送到下一页的解决方法
2023-11-15 01:41:29
MySQL表字段设置默认值(图文教程及注意细节)
2024-01-18 14:34:52
![](https://img.aspxhome.com/file/2023/6/126266_0s.gif)
mysql实现将字符串字段转为数字排序或比大小
2024-01-16 19:59:16
![](https://img.aspxhome.com/file/2023/2/99502_0s.png)
Golang 编译成DLL文件的操作
2024-05-09 09:55:30
python树莓派红外反射传感器
2022-12-05 16:11:47
![](https://img.aspxhome.com/file/2023/9/89399_0s.jpg)
史上最简单的MySQL数据备份与还原教程(下)(三十七)
2024-01-25 06:03:33
![](https://img.aspxhome.com/file/2023/7/118227_0s.png)
python pycharm最新版本激活码(永久有效)附python安装教程
2022-08-16 14:08:56
![](https://img.aspxhome.com/file/2023/0/89220_0s.png)
php生成随机密码的三种方法小结
2023-11-22 04:25:50
多维度导航探秘II
2010-08-17 21:24:00
![](https://img.aspxhome.com/file/UploadPic/20108/17/qqe59586e59f8e-53s.png)
python实现图书借阅系统
2022-03-26 07:46:32
使用Python获取网段IP个数以及地址清单的方法
2021-02-25 03:28:21
![](https://img.aspxhome.com/file/2023/2/114552_0s.jpg)
微信小程序开发实例详解
2022-11-07 19:55:48
![](https://img.aspxhome.com/file/2023/6/131776_0s.jpg)
Mysql Innodb引擎优化(参数篇)
2010-05-02 19:57:00
python学习实操案例(五)
2023-08-22 01:52:43
![](https://img.aspxhome.com/file/2023/0/92220_0s.png)