@ssports_fe/ssutils
Version:
20 lines (17 loc) • 567 B
JavaScript
/**
* @file 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
* @date 2018-03-01
* @author sunshaocheng
*/
/**
* 返回Number类型数字的长度
* @param {*number} num Input number
*/
const digitLength = (num) => {
// Get digit length of e
const eSplit = num.toString().split(/[eE]/);
const len = (eSplit[0].split('.')[1] || '').length - (Number(eSplit[1] || 0));
return len > 0 ? len : 0;
};
module.exports = digitLength;