当前位置:网站首页>在小程序中关于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'
使用
结果
方式二:
边栏推荐
猜你喜欢
lingo入门——河北省第三届研究生建模竞赛B题
Late night drinking, 50 classic SQL questions, really fragrant~
VSCode编写OpenCV
LeetCode practice and self-comprehension record (1)
摆脱极域软件的限制
config.js related configuration summary
NB-IOT智能云家具项目系列实站
Teach you simple steps to achieve industrial raspberries pie properly installed RS232 USB drive
LeetCode练习及自己理解记录(1)
D45_Camera assembly Camera
随机推荐
numpy.random usage documentation
UI刘海屏适配方式
js 使用雪花id生成随机id
深入分析若依数据权限@datascope (注解+AOP+动态sql拼接) 【循序渐进,附分析过程】
The cocos interview answers you are looking for are all here!
input detailed file upload
Tencent Internal Technology: Evolution of Server Architecture of "The Legend of Xuanyuan"
七种让盒子水平垂直居中的方法
Transformer详细解读与预测实例记录
Q 2020, the latest senior interview Laya soul, do you know?
Passing parameters in multiple threads
Met with the browser page
Chengyun Technology was invited to attend the 2022 Alibaba Cloud Partner Conference and won the "Gathering Strength and Going Far" Award
Successful indie developers deal with failure & imposters
数组&的运算
亚马逊美国站:马术头盔CPC认证标准要求
Difference between link and @improt
人人AI(吴恩达系列)
文本样式这一篇文章就够了
DevOps流程demo(实操记录)