UNPKG

xe-utils

Version:

JavaScript 函数库、工具类

30 lines (28 loc) • 1.32 kB
import helperMultiply from './helperMultiply.js';import helperMathAbs from './helperMathAbs.js';import helperStringSubstring from './helperStringSubstring.js';import helperMathPow from './helperMathPow.js';import toNumber from './toNumber.js';import toNumberString from './toNumberString.js';function helperCreateMathNumber (name, isRoundFn) { return function (num, decimalPlaces, awayZero) { var numRest = toNumber(num) var rest = numRest if (numRest) { decimalPlaces = decimalPlaces >> 0 var numStr = toNumberString(numRest) var nums = numStr.split('.') var intStr = nums[0] var floatStr = nums[1] || '' var fStr = helperStringSubstring(floatStr, 0, decimalPlaces + 1) var subRest = intStr + (fStr ? ('.' + fStr) : '') if (decimalPlaces >= floatStr.length) { return toNumber(subRest) } subRest = numRest if (decimalPlaces > 0) { var ratio = helperMathPow(10, decimalPlaces) var tmplNum = helperMultiply(subRest, ratio) rest = isRoundFn && subRest < 0 && awayZero !== false ? -(Math[name](helperMathAbs(tmplNum)) / ratio) : (Math[name](tmplNum) / ratio) } else { rest = Math[name](subRest) } } return rest } } export default helperCreateMathNumber