当前位置:网站首页>在小程序中关于js数字精度丢失的解决办法
在小程序中关于js数字精度丢失的解决办法
2022-08-05 05:29:00 【weixin_43923808】
我们在计算 0.1 + 0.1 正确结果是 0.2,但是计算 0.1 + 0.2 的结果并不是0.3,而是0.30000000000000004,0.58*100应该是58却显示57.999999999999999
这是由于
JS 数字丢失精度的原因
方式一:
var aa=parseFloat((0.1 + 0.2).toPrecision(12))//12为保留几位小数
var bb=parseFloat((0.58 * 100).toPrecision(12))
console.log(aa, 'aa')
console.log(bb, 'bb')
方式二:(推荐)
解决方式:把小数放到位整数(乘倍数),再缩小回原来倍数(除倍数)
//如: 0.1 + 0.2
(0.1*10 + 0.2*10) / 10 == 0.3 // true
我在utils文件夹下新建public_fun.js一个作为公用文件
public_fun.js内容如下
/*
* 判断obj是否为一个整数
*/
function isInteger(obj) {
return Math.floor(obj) === obj
}
/*
* 将一个浮点数转成整数,返回整数和倍数。如 3.14 >> 314,倍数是 100
* @param floatNum {number} 小数
* @return {object}
* {times:100, num: 314}
*/
function toInteger(floatNum) {
var ret = { times: 0, num: 0 }
if (isInteger(floatNum)) {
ret.num = floatNum
return ret
}
var strfi = floatNum + ''
var dotPos = strfi.indexOf('.')
var len = strfi.substr(dotPos + 1).length
var times = Math.pow(10, len)
var intNum = parseInt(floatNum * times + 0.5, 10)
ret.times = times
ret.num = intNum
return ret
}
/*
* 核心方法,实现加减乘除运算,确保不丢失精度
* 思路:把小数放大为整数(乘),进行算术运算,再缩小为小数(除)
*
* @param a {number} 运算数1
* @param b {number} 运算数2
* @param digits {number} 精度,保留的小数点数,比如 2, 即保留为两位小数
* @param op {string} 运算类型,有加减乘除(add/subtract/multiply/divide)
*
*/
function operation(a, b, digits, op) {
var o1 = toInteger(a)
var o2 = toInteger(b)
var max = o1.times > o2.times ? o1.times : o2.times
var result = null
switch (op) {
case 'add':
result = o1.num + o2.num
break
case 'subtract':
result = o1.num - o2.num
break
case 'multiply':
result = o1.num * o2.num
break
case 'divide':
result = o1.num / o2.num
break
}
return result / max
}
// 加减乘除的四个接口
function add(a, b, digits) {
return operation(a, b, digits, 'add')
}
function subtract(a, b, digits) {
return operation(a, b, digits, 'subtract')
}
function multiply(a, b, digits) {
return operation(a, b, digits, 'multiply')
}
function divide(a, b, digits) {
return operation(a, b, digits, 'divide')
}
export {
add,
subtract,
multiply,
divide
}
使用:
在需要用到的页面导入
import * as floatNum from '@/utils/public_fun'
使用
结果
方式二:
边栏推荐
猜你喜欢
Complete mysql offline installation in 5 minutes
scikit-image image processing notes
VRRP overview and experiment
多用户商城多商户B2B2C拼团砍价秒杀支持小程序H5+APP全开源
Error correction notes for the book Image Processing, Analysis and Machine Vision
多行文本省略
DevOps流程demo(实操记录)
Drools规则引擎快速入门(一)
The hook of the operation of the selenium module
System basics - study notes (some command records)
随机推荐
D46_Force applied to rigid body
The hook of the operation of the selenium module
网络协议基础-学习笔记
DevOps - Understanding Learning
Cloud Computing Basics - Study Notes
One-arm routing experiment and three-layer switch experiment
UI刘海屏适配方式
Cocos Creator Mini Game Case "Stick Soldier"
selenium学习
Matplotlib plotting notes
Mina disconnects and reconnects
cs231n learning record
Media query, rem mobile terminal adaptation
Get the network input dimensions of the pretrained model
Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
Tencent Internal Technology: Evolution of Server Architecture of "The Legend of Xuanyuan"
After docker is deployed, mysql cannot connect
Error correction notes for the book Image Processing, Analysis and Machine Vision
Configuration of routers and static routes
记录vue-页面缓存问题