当前位置:网站首页>JS计算精度问题和及数据格式
JS计算精度问题和及数据格式
2022-07-16 08:01:00 【皮蛋solo周呀】
提示:这里记录前端js数据计算一些问题
JS计算精度问题
一、在vue中,写一个js方法,如下precisionMath.js
export const precisionMath = {
methods: {
// 加法
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
//动态控制精度长度
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m + arg2 * m) / m).toFixed(n);
},
// 除法
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(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(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
//动态控制精度长度
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
}
}
二、在页面中使用
import {
precisionMath } from "@/js/precisionMath"; // 在页面中引入这个js文件
// precisionMath.methods.方法名称(参数1,参数2)
保留小数点后几位数据
retain(num, d) {
return (parseInt(num * 100) / 100).toFixed(d)
},
this.retain('数值','保留小数位数')
边栏推荐
- Ciphertext on RGB image -- illegal data hiding
- Realize chat room based on epoll (including timer to handle customer connection status)
- Metaltc5.0 implements webrtc version IPC
- Mobile communication helps the new move of summer grain storage, and scientific and technological means are more effective
- leetcode:378. The k-th smallest element in an ordered matrix
- 从一篇防范钓鱼邮件的通知说起
- Kingbasees v8r6 cluster backup recovery case - the backup database performs physical backup as a repo host
- C语言求回文质数
- This domestic editor will open source soon!
- Page break before \ page break inside \ page break after usage
猜你喜欢

page-break-before\page-break-inside\page-break-after用法

High performance timer

【LeetCode】9. Flood fill · image rendering

Airiot low code development platform, 10 minutes to build the Internet of things system

JS Object.keys()

CV2. Setmousecallback() displays the pixel value and position of the mouse click image

Application layer of OSI model of RS485 interface

iowait 理解

Free SSL certificate application and deployment practice

Analysis on the necessity and key functions of the construction of video monitoring platform for comprehensive building
随机推荐
Find active SQL connections in SQL Server
High performance timer
Top n% data before SQL calculation
The most complete MySQL in history! Desperately tidy up
A lightweight tracking method based on unique identifier of timestamp
Chapter 3: runtime data area - independent space
笔记-如何在稀烂的数据中做深度学习
MANUAL_FLUSH is enabled but the buffer is too big 解决办法 及 产生原因分析
async函数实现多个请求处理
PostgreSQL is now installed
Implement chat room based on poll
zabbix详细介绍
What is a recommendation system?
Analysis on the necessity and key functions of the construction of video monitoring platform for comprehensive building
Well known to do apps at home and abroad
Remove duplicate elements in the list max min key
【LeetCode】11. Lowest common ancestor of a binary search tree
AcWing 135. 最大子序和
高性能定时器
Image compression based on Base64 and blob