UNPKG

@daysnap/utils

Version:
46 lines (44 loc) 1.17 kB
// src/round.ts function round(val, fractionDigits) { const number = val; if (number >= 10 ** 21) { return number.toString(); } if (typeof fractionDigits === "undefined" || fractionDigits === 0) { return Math.round(number).toString(); } let result = number.toString(); const arr = result.split("."); if (arr.length < 2) { result += "."; for (let i = 0; i < fractionDigits; i += 1) { result += "0"; } return result; } const integer = arr[0]; const decimal = arr[1]; if (decimal.length === fractionDigits) { return result; } if (decimal.length < fractionDigits) { for (let i = 0; i < fractionDigits - decimal.length; i += 1) { result += "0"; } return result; } result = `${integer}.${decimal.substr(0, fractionDigits)}`; let last = decimal.substr(fractionDigits, 1); if (/^\d(9){5,}[0-9]$/.test(decimal.substr(fractionDigits))) { last = (+last + 1).toString(); } if (parseInt(last, 10) >= 5) { const x = 10 ** fractionDigits; result = (Math.round(parseFloat(result) * x) + 1) / x; result = round(result, fractionDigits); } return result; } export { round };