UNPKG

@yoroi/common

Version:

The Common package of Yoroi SDK

35 lines (34 loc) 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.atomicFormatter = atomicFormatter; var _bignumber = _interopRequireDefault(require("bignumber.js")); var _atomicBreakdown = require("./atomic-breakdown"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /** * @description Keep the result to display only, internally use bigint, * otherwise it will need to localize converters * Formats a bigint to a string with the given number of decimal places. * @param {Object} options The options to use for formatting. * @param options.value The bigint to format. * @param options.decimalPlaces The number of decimal places to include. * @param options.format The locale format to use for formatting the parsed value. * @returns The formatted string. * * @example * atomicFormatter({ value: 123456789000000000000000001n, decimalPlaces: 18 }) // => '123,456,789.000000000000000001' * atomicFormatter({ value: 123456789n, decimalPlaces: 0 }) // => '123,456,789' * atomicFormatter({ value: 1_000_000_000_000_000_000n, decimalPlaces: 18 }) // => '1.000000000000000000' * atomicFormatter({ value: 12345n, decimalPlaces: 2 }) // => '123.45' * atomicFormatter({ value: 12345678n, decimalPlaces: 5 }) // => '123.45678' */ function atomicFormatter(_ref) { let { value, decimalPlaces, format } = _ref; return (0, _atomicBreakdown.atomicBreakdown)(value, decimalPlaces).bn.toFormat(decimalPlaces, _bignumber.default.ROUND_DOWN, format); } //# sourceMappingURL=atomic-formatter.js.map