js 处理四则运算失去精度问题
计算精度,特别是浮点数计算。举个栗子

然后我寻求各种解决办法,其中用了lodash,ramda提供的四则运算。结果发现并没有完全的解决这个问题, 怎么办呢?
然后我各种百度借鉴大神的解决办法, 一波三折又发现一个问题。大神在封装四则运算的时候, 用了toFixed 方法。然而这个方法也有坑,举个栗子。

所以通过上图我们发现 类似这种2.55 这个变态数字取特殊小数位的时候和其他不一样。
经过的不断的研究和测试最终 封装了五个方法下面贴源代码:





如果使用npm 的同学可以这样
npm i fourth_calculate --save
import Calculate from fourth_calculat
调用 Calculate.toFixed、Calculate.Add() 等等
-- 如果只想复杂的同学 请移步 下面的链接:
https://github.com/yuhaiyang1/float_calculate/blob/master/lib/index.js
代码 github地址 :https://github.com/yuhaiyang1/float_calculate
其中这几个方法都写了单元测试 有兴趣的同学可以看下,也可以关注我,我会持续把我遇到的坑点分享给大家的 ~~ 想打赏我也不会拒绝
网友评论