关于js/vue小数点失精算法修正
1.parseInt,parseFloat
大多数情况下不需要小数点后很多位,可以使用toFixed(n)方法修正后(n是小数后精确的位数)。
console.log(0.119*100); // 11.899999999999999 parseFloat((0.119*100).toFixed(10)); // 11.9
2.计算出小数点后位数的大的一个,乘法消去小数点做整数运算后做除法
Math.round(arg1 * m - arg2 * m) / m Math.round(( 0.119 *100)*1000)/1000; // 11.9
还可以用 位运算符、转化为字符串处理(Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m))、第三方插件库(big.js,bignumber.js,decimal.js等等)等方式进行处理。
既然找到解决方式,在Vue中使用就可
<td>{undefined{data | getPercentage}}%</td> 1.Vue.filter("getPercentage", function(value) { //全局方法 Vue.filter() 注册一个自定义过滤器,必须放在Vue实例化前面 return Math.round((value * 100)*1000)/1000; }); 2.computed:{ getPercentage:function () { return data* 100 } }
原文链接:https://blog.csdn.net/qq_28073073/article/details/89847046