当前位置:网站首页>Solve the problem of JS calculation accuracy
Solve the problem of JS calculation accuracy
2022-04-23 05:20:00 【Lora_ 0925】
// Divide
accDiv(arg1, arg2) {
let 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);
},
// multiply
accMul(arg1, arg2) {
let 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)
},
// reduce
accSub(arg1, arg2) {
let 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));
// Dynamic control precision length
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
},
// Add
accAdd(arg1, arg2) {
let 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
}
版权声明
本文为[Lora_ 0925]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220546439692.html
边栏推荐
- 2021-11-08
- Three 之 three.js (webgl)模型的删除/场景的清空/内存的释放 的简单整理
- Where, on when MySQL external connection is used
- My old programmer's perception of the dangers and opportunities of the times?
- 狼叔来找翻译人员了--plato--持续翻译中.....
- 开源规则引擎——ice:致力于解决灵活繁复的硬编码问题
- 2021-10-25
- Redis lost key and bigkey
- 使用 Kears 实现ResNet-34 CNN
- 史上最强egg框架的error处理机制
猜你喜欢
随机推荐
Some experience in using MySQL / tidb database [slowly updating...]
2021-11-01
calendar. Pit point of getactualmaximum (calendar. Day_of_month)
Study notes: unity customsrp-10-point and spot shadows
Redis persistence
One month countdown, pgconf What are the highlights of the latest outlook of asia2021 Asian Conference?
使用zerotier让异地设备组局域网
日志简介和构建web应用
领域驱动模型DDD(三)——使用Saga管理事务
varnish入门
Interview summary
MFC implementation resources are implemented separately by DLL
2021-10-25
Cloud computing and cloud native architecture design of openshift
Laravel [view]
Semi synchronous replication of MariaDB
Where, on when MySQL external connection is used
Three of three JS (WEB GL) model deletion / scene emptying / simple sorting of memory release
青岛敏捷之旅,来了!
Laravel routing job