当前位置:网站首页>JS calculation accuracy and data format
JS calculation accuracy and data format
2022-07-18 15:52:00 【Preserved egg solo week】
Tips : Record the front end here js Some problems in data calculation
List of articles
JS The accuracy of calculation
One 、 stay vue in , Write a js Method , as follows precisionMath.js
export const precisionMath = {
methods: {
// Add
add(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));
//last modify by deeka
// Dynamic control precision length
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m + arg2 * m) / m).toFixed(n);
},
// division
except(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);
},
// ride
ride(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)
},
// reduce
reduce(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));
//last modify by deeka
// Dynamic control precision length
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
}
}
Two 、 Used on the page
import {
precisionMath } from "@/js/precisionMath"; // Introduce this in the page js file
// precisionMath.methods. Method name ( Parameters 1, Parameters 2)
Keep several decimal places
retain(num, d) {
return (parseInt(num * 100) / 100).toFixed(d)
},
this.retain(' The number ',' Keep the decimal places ')
边栏推荐
- redis集群测试
- MRP的基本任务是什么
- leetcode:378. The k-th smallest element in an ordered matrix
- 笔记-如何在稀烂的数据中做深度学习
- eoc是什么
- Specify TLS 1.3 and ciphers to improve security and performance
- Page break before \ page break inside \ page break after usage
- Calculate the Euclidean distance between the row vectors of two matrices
- 新功能上线需要发版本,回归测试中总是出现之前没有的问题
- The problem and solution of calling glcreateshader to crash
猜你喜欢

Iowait understanding

网页制作(二)

MRP的基本任务是什么

Minuterie haute performance

Example of declarative transaction management

eoc是什么
![[training Day2] cinema ticket [combinatorics] [Cartland number]](/img/39/a87cef4d6dc1b393b78f7fa4f71be1.png)
[training Day2] cinema ticket [combinatorics] [Cartland number]

Error establishing connection between MySQL and idea

Rotation in ue4/5: three Euler angles picth, yaw, roll and frotator

Blue Hat Cup 2022 preliminaries electronic forensics
随机推荐
Do you know the shell foundation often asked in interviews?
AcWing 133. earthworm
The problem and solution of calling glcreateshader to crash
免费SSL证书申请及部署实践
cv2.setMouseCallback()显示鼠标点击图片的像素值和位置
怎么设置域名解析?
如何用断言思路优雅地处理代码异常
Detailed explanation of Flink resource management
redis 配置,集群安装与扩容
eoc是什么
Codeforces Round #583 (Div. 1 + Div. 2) - A, D, E
ESRI launches indoor positioning system for facility routing
osgEarth的Rex引擎原理分析(一二八)rex的引擎和图层投影及其关系
Example of declarative transaction management
面试常问的shell基础,你会了吗?
C language -- the implementation of common string functions
1111111111111
leetcode:378. The k-th smallest element in an ordered matrix
[training Day2] torch bearer [violence] [DFS]
ncnn 编译与使用 pnnx 编译与使用