(number, times)
| 1 | // 放大指定倍数取整 |
| 2 | const toInteger = function (number, times) { |
| 3 | let maxDecimalDigits = 0 |
| 4 | // 如果是科学计数法 |
| 5 | if (String(number).indexOf("e") !== -1) { |
| 6 | number = scientificNotationToString(number) |
| 7 | } |
| 8 | // 计算小数位数 |
| 9 | try { |
| 10 | maxDecimalDigits = Math.max(maxDecimalDigits, String(number).split(".")[1].length) |
| 11 | } catch (e) { |
| 12 | maxDecimalDigits = 0 |
| 13 | } |
| 14 | // 位数不足补0 |
| 15 | let mantissa = '' |
| 16 | for (let i = 0; i < times - maxDecimalDigits; i++) { |
| 17 | mantissa += '0' |
| 18 | } |
| 19 | // 返回结果 |
| 20 | if (maxDecimalDigits < times) { |
| 21 | return Number(String(number).replace(".", "") + mantissa) |
| 22 | } else { |
| 23 | return Number(String(number).replace(".", "")) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // 计算最长小数位数 |
| 28 | const getMaxDecimalDigits = function () { |
no test coverage detected