当前位置:网站首页>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
边栏推荐
猜你喜欢

Solution of how to log in with mobile phone verification code in wireless network

Low code and no code considerations

mariadb数据库的主从复制

Redis persistence

One month countdown, pgconf What are the highlights of the latest outlook of asia2021 Asian Conference?

Minimum spanning tree -- unblocked project hdu1863

Cloud computing and cloud native architecture design of openshift

Data security has become a hidden danger. Let's see how vivo can make "user data" armor again

phphphphphphphp

SQLyog的基本使用
随机推荐
Three of three JS (webgl) simple sorting of rotation attribute function, and a simple case of rotating around the axis based on this
Data management of basic operation of mairadb database
MySQL external connection, internal connection, self connection, natural connection, cross connection
Discussion on flow restriction
Five key technologies to improve the devsecops framework
Redis data type usage scenario
Routing parameters
Minimum spanning tree -- unblocked project hdu1863
改进DevSecOps框架的 5 大关键技术
MySQL foreign key constraint
Traversal of tree
C test calls the paddlesharp module to recognize pictures and words
WTL 自绘控件库 (CQsCheckComboxBox)
MySQL basics 3
npm升级后问题,慌得一批
TSlint注释忽略错误和RESTful理解
Detailed explanation of concurrent topics
Redis的基本知识
Power consumption parameters of Jinbei household mute box series
Laravel routing settings