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

结果

方式二:
边栏推荐
猜你喜欢

Late night drinking, 50 classic SQL questions, really fragrant~

DevOps流程demo(实操记录)

sql server duplicate values are counted after

NB-IOT智能云家具项目系列实站

Transformer interprets and predicts instance records in detail

txt文件英语单词词频统计

input detailed file upload

前置++和后置++的区别

The cocos interview answers you are looking for are all here!
![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]
随机推荐
网络协议基础-学习笔记
document.querySelector() method
The cocos interview answers you are looking for are all here!
DevOps process demo (practical record)
数组&的运算
Programmers should understand I/O this way
reduce()方法的学习和整理
Q 2020, the latest senior interview Laya soul, do you know?
记录vue-页面缓存问题
【FAQ】CCAPI兼容EOS相机列表(2022年8月 更新)
【FAQ】What is Canon CCAPI
H5 的浏览器存储
Cloud Computing Basics - Study Notes
H5开发调试-Fiddler手机抓包
numpy.random usage documentation
Drools规则引擎快速入门(一)
selenium learning
Alibaba Cloud Video on Demand
农场游戏果园系统+牧场养殖系统+广告联盟模式流量主游戏小程序APP V1
The 25 best free games on mobile in 2020