@0xtorch/big-decimal
Version:
An arbitrary precision Decimal type for TypeScript that extends BigInt.
28 lines • 1.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.toStringBigDecimal = void 0;
const toStringBigDecimal = ({ value, decimals }) => {
if (value === 0n) {
return '0';
}
const isNegative = value < 0;
const prefix = isNegative ? '-' : '';
const absValue = isNegative ? -value : value;
const valueText = absValue.toString();
const valueLength = valueText.length;
const decimalLength = decimals;
if (valueLength <= decimalLength) {
const text = `0.${valueText.padStart(decimalLength, '0')}`;
const formattedText = text.replace(/0+$/, '');
return `${prefix}${formattedText}`;
}
const intText = valueText.slice(0, valueLength - decimalLength);
const decimalText = valueText.slice(valueLength - decimalLength);
const formattedDecimalText = decimalText.replace(/0+$/, '');
if (formattedDecimalText.length === 0) {
return `${prefix}${intText}`;
}
return `${prefix}${intText}.${formattedDecimalText}`;
};
exports.toStringBigDecimal = toStringBigDecimal;
//# sourceMappingURL=toStringBigDecimal.js.map