@yoroi/common
Version:
The Common package of Yoroi SDK
30 lines (29 loc) • 1.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.atomicBreakdown = atomicBreakdown;
var _bignumber = _interopRequireDefault(require("bignumber.js"));
var _immer = require("immer");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function atomicBreakdown(bigInt, decimalPlaces) {
const scale = BigInt(10) ** BigInt(decimalPlaces);
const isNegative = bigInt < 0;
const sign = isNegative ? '-' : '';
const signAdjust = isNegative ? -1n : 1n;
const absoluteBigInt = signAdjust * bigInt;
const integer = (absoluteBigInt / scale).toString();
const fraction = decimalPlaces > 0 ? (absoluteBigInt % scale).toString().padStart(decimalPlaces, '0') : '';
const separator = decimalPlaces > 0 ? '.' : '';
const str = `${sign}${integer}${separator}${fraction}`;
const bn = new _bignumber.default(str);
return (0, _immer.freeze)({
decimalPlaces,
bi: bigInt,
integer,
fraction,
bn,
str
});
}
//# sourceMappingURL=atomic-breakdown.js.map