MCPcopy Create free account
hub / github.com/LiuXin-Developer/easyMath / toInteger

Function toInteger

src/index.js:2–25  ·  view source on GitHub ↗
(number, times)

Source from the content-addressed store, hash-verified

1// 放大指定倍数取整
2const 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// 计算最长小数位数
28const getMaxDecimalDigits = function () {

Callers 4

divisionFunction · 0.85
multiplicationFunction · 0.85
subtractionFunction · 0.85
additionFunction · 0.85

Calls 1

Tested by

no test coverage detected