一。加法精度修改后如下:
function add(...val) { let max = 0 let count = 0 for (let i = 0; i < val.length; i++) { const strVal = val[i].toString() const index = strVal.indexOf('.') let num = 0 if (index > -1) { num = strVal.length - 1 - index max = num > max ? num : max } } for (let i = 0; i < val.length; i++) { count += Math.round(val[i] * Math.pow(10, max)) } return count / Math.pow(10, max) }
使用:add(0.1, 0.2, 0.3, 0.4) => 1。可以传多个参数进行相加。
二。减法精度修改后如下:
function sub(...val) { let max = 0 let count = val[0] | 0 for (let i = 0; i < val.length; i++) { const strVal = val[i].toString() const index = strVal.indexOf('.') let num = 0 if (index > -1) { num = strVal.length - 1 - index max = num > max ? num : max } } for (let i = 0; i < val.length; i++) { count -= Math.round(val[i] * Math.pow(10, max)) } return count / Math.pow(10, max) }
使用:sub(1, 0.2, 0.3, 0.4) => 0.1。相当于1 - 0.2 -0.3 -0.4;可以传多个参数,使用首位减后面的所有参数。
三。乘法精度修改后如下:
function ride(...val) { const strVal = val[0].toString() const strValTwo = val[1].toString() const index = strVal.indexOf('.') const indexTwo = strValTwo.indexOf('.') const num = [0, 0] if (index > -1) { num[0] = strVal.length - 1 - index } if (indexTwo > -1) { num[1] = strValTwo.length - 1 - index } return Math.round((val[0] * Math.pow(10, num[0])) * (val[1] * Math.pow(10, num[1]))) / Math.pow(10, num[0] + num[1]) }
使用:ride(0.5, 0.6) => 3, 只允许传入两个参数。%计算可以这样ride(0.5, 100) => 50。
function percentage(val) { const strVal = val.toString() const index = strVal.indexOf('.') let num = 0 if (index > -1) { num = strVal.length - 1 - index } if (num > 2) { return Math.round(val * Math.pow(10, num)) / Math.pow(10, num - 2) } else { return Math.round(val * 100) } }
上面这个是专门用于计算%数的。percentage(0.05) => 5
四。除法精度修改后如下:
function exc(val, valTwo = 100) { const strVal = val.toString() const strValTwo = valTwo.toString() const index = strVal.indexOf('.') const indexTwo = strValTwo.indexOf('.') const num = [0, 0] if (index > -1) { num[0] = strVal.length - 1 - index } if (indexTwo > -1) { num[1] = strValTwo.length - 1 - index } return Math.round(val * Math.pow(10, num[0])) / (Math.round((valTwo * Math.pow(10, num[1]))) * Math.pow(10, num[0] - num[1])) }
使用:exc(0.5, 0.2) => 2.5, 只允许传入两个参数。如果计算出现无穷数请后期根据需要修改最后代码进行取舍。
建议:所有的计算最后都由后端处理好,再传到前端,这样可以避免精度问题。
相关推荐
在javascript中,当你使用小数进行加减乘除运算时,你会发现,所得到的结果有时后面带有长长的一段小数,使运算变得复杂,并且影响计算结果。上网查询了一下原因,大致如下:在javascript中,带小数的数据运算时总会...
JS小数加减乘除运算后小数点后产生多位数和计算精度损失的解决方案
主要介绍了javascript解决小数的加减乘除精度丢失的方案的相关资料以及JavaScript中关于丢失数字精度的问题的探讨,非常的详细,需要的朋友可以参考下
javascript解决小数的加减乘除精度丢失的方案.docx
解决JS浮点数(小数)计算加减乘除的BUG
JS精准加减乘除算法,解决JS原生加减乘除运算精度不足问题。
主要介绍了js加减乘除精确计算,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
JS精度问题,解决了JS加减乘除时候精度错误问题,就是后面带有很多0的问题
在金额的加减乘除运算时我们往往会忽略计算结果的精度问题,导致莫名奇妙出现了过多的小数位,这在我做银行项目时尤其需要关注的一个问题点。 其实在我们使用正常的 + - * / 运算时,在某些情况下就会出现精度丢失...
javascript(js)的小数点加减乘除问题,是一个js的bug如0.3*1 = 0.2999999999等,下面列出可以完美求出相应精度的四种js算法 function accDiv(arg1,arg2){ var t1=0,t2=0,r1,r2; try{t1=arg1.toString().split(.)...
floatOPS 包含加减乘除四个方法,能确保浮点数运算不丢失精度
JS入门级四则运算,仅支持加减乘除,经测试可以计算结果为9位以内的小数的四则运算,失精度的概率小,请留下改进建议
可以计算表达式,同时可以使用加减乘除,括号,三角函数等功能,并且解决了精度问题
主要介绍了JavaScript使用math.js进行精确计算操作,结合实例形式分析了开源库math.js进行高精度数学运算相关操作技巧,需要的朋友可以参考下
微信小程序写的计算器,其中有加减乘除,清除,后退,记忆的功能,解决了JS浮点运算时的精度问题。如果有问题,可以邮箱联系gaofto@126.com
简易的html计算器,可实现加减乘除和科学计算器功能。由于计算部分使用JavaScript原生四则运算,所以精度不可靠。