UNPKG

@ssports_fe/ssutils

Version:
18 lines (15 loc) 547 B
/** * @file 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。 * 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998 * @date 2018-03-01 * @author sunshaocheng */ /** * 精确除法 */ const divide = (num1, num2) => { const num1Changed = Number(num1.toString().replace('.', '')); const num2Changed = Number(num2.toString().replace('.', '')); return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1))); }; module.exports = divide;