当前位置:网站首页>在小程序中关于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'使用

结果

方式二:
边栏推荐
- VSCode编写OpenCV
- One-arm routing experiment and three-layer switch experiment
- 深夜小酌,50道经典SQL题,真香~
- LaTeX uses frame to make PPT pictures without labels
- 获取预训练模型的网络输入尺寸
- DevOps process demo (practical record)
- docker部署完mysql无法连接
- 系统基础-学习笔记(一些命令记录)
- Programmers should understand I/O this way
- Passing parameters in multiple threads
猜你喜欢

Transformer详细解读与预测实例记录

sql server duplicate values are counted after

Cloud Computing Basics - Study Notes

The 25 best free games on mobile in 2020
![In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]](/img/b5/03f55bb9058c08a48eae368233376c.png)
In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]

NACOS配置中心设置配置文件

超简单的白鹭egret项目添加图片详细教程

设置文本向两边居中展示

Collision, character controller, Cloth components (cloth), joints in the Unity physics engine

滚动条问题,未解决
随机推荐
无法导入torchvision.io.read_image
淘宝宝贝页面制作
scikit-image image processing notes
Matplotlib绘图笔记
In-depth analysis if according to data authority @datascope (annotation + AOP + dynamic sql splicing) [step by step, with analysis process]
【FAQ】CCAPI Compatible EOS Camera List (Updated in August 2022)
【考研结束第一天,过于空虚,想对自己进行总结一下】
【FAQ】CCAPI兼容EOS相机列表(2022年8月 更新)
Dry!Teach you to use industrial raspberries pie combining CODESYS configuration EtherCAT master station
el-progress implements different colors of the progress bar
Browser Storage for H5
scikit-image图像处理笔记
numpy.random usage documentation
Detailed explanation of the construction process of Nacos cluster
Seven Ways to Center a Box Horizontally and Vertically
盒子模型大详解
wc, grep, tar, vi/vim
Collision, character controller, Cloth components (cloth), joints in the Unity physics engine
The size of the screen adaptation
浏览器兼容汇总