1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- /* eslint-disable max-classes-per-file */
- import BigIntDecimal from "./BigIntDecimal";
- import NumberDecimal from "./NumberDecimal";
- import { trimNumber } from "./numberUtil";
- import { supportBigInt } from "./supportUtil";
- // Still support origin export
- export { NumberDecimal, BigIntDecimal };
- export default function getMiniDecimal(value) {
- // We use BigInt here.
- // Will fallback to Number if not support.
- if (supportBigInt()) {
- return new BigIntDecimal(value);
- }
- return new NumberDecimal(value);
- }
- /**
- * Align the logic of toFixed to around like 1.5 => 2.
- * If set `cutOnly`, will just remove the over decimal part.
- */
- export function toFixed(numStr, separatorStr, precision) {
- var cutOnly = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
- if (numStr === '') {
- return '';
- }
- var _trimNumber = trimNumber(numStr),
- negativeStr = _trimNumber.negativeStr,
- integerStr = _trimNumber.integerStr,
- decimalStr = _trimNumber.decimalStr;
- var precisionDecimalStr = "".concat(separatorStr).concat(decimalStr);
- var numberWithoutDecimal = "".concat(negativeStr).concat(integerStr);
- if (precision >= 0) {
- // We will get last + 1 number to check if need advanced number
- var advancedNum = Number(decimalStr[precision]);
- if (advancedNum >= 5 && !cutOnly) {
- var advancedDecimal = getMiniDecimal(numStr).add("".concat(negativeStr, "0.").concat('0'.repeat(precision)).concat(10 - advancedNum));
- return toFixed(advancedDecimal.toString(), separatorStr, precision, cutOnly);
- }
- if (precision === 0) {
- return numberWithoutDecimal;
- }
- return "".concat(numberWithoutDecimal).concat(separatorStr).concat(decimalStr.padEnd(precision, '0').slice(0, precision));
- }
- if (precisionDecimalStr === '.0') {
- return numberWithoutDecimal;
- }
- return "".concat(numberWithoutDecimal).concat(precisionDecimalStr);
- }
|