当前位置:网站首页>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
边栏推荐
- Domain driven model DDD (III) -- using saga to manage transactions
- Data management of basic operation of mairadb database
- phphphphphphphp
- 日志简介和构建web应用
- Locks and transactions in MySQL
- To understand Devops, you must read these ten books!
- Discussion on flow restriction
- !!!!!!!!!!!!!!!!!!
- PIP free export with path (@ file: / / /) notes
- Basic knowledge of vegetable chicken database
猜你喜欢

Graphics.FromImage报错“Graphics object cannot be created from an image that has an indexed pixel ...”

Nacos source code startup error report solution

青岛敏捷之旅,来了!

Using PHP post temporary file mechanism to upload arbitrary files

Three of three JS (webgl) is simple to draw lines / arcs according to points (based on linegeometry / line2 / linematerial, draw two arc segments based on the center of the circle)

无线网怎么用手机验证码登录解决方案

MySQL basics 3

Qingdao agile tour, coming!

Discussion on flow restriction

One month countdown, pgconf What are the highlights of the latest outlook of asia2021 Asian Conference?
随机推荐
Top 25 Devops tools in 2021 (Part 2)
Laravel routing settings
MySQL foreign key constraint
Solution of how to log in with mobile phone verification code in wireless network
Let the LAN group use the remote device
好的测试数据管理,到底要怎么做?
Jupyter notebook crawling web pages
无线网怎么用手机验证码登录解决方案
Study notes: unity customsrp-11-post processing --- bloom
Chapter III project schedule management of information system project manager summary
The concept of meta universe is popular. Is virtual real estate worth investing
4 most common automated test challenges and Countermeasures
egg中的cors和proxy(づ ̄3 ̄)づ╭~踩坑填坑的过程~ToT~
Streamexecutionenvironment of Flink source code
Basic theory of Flink
How to add beautiful code blocks in word | a very complete method to sort out and compare
使用zerotier让异地设备组局域网
MySQL external connection, internal connection, self connection, natural connection, cross connection
云计算与云原生 — OpenShift 的架构设计
Implementation of resnet-34 CNN with kears