@yoroi/common
Version:
The Common package of Yoroi SDK
31 lines (29 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.atomicToDecimal = atomicToDecimal;
var _bignumber = require("bignumber.js");
const pattern = /\d+/g;
/**
* Converts an atomic value to its decimal representation.
*
* @param {Object} options - The options for the conversion.
* @param {string} options.value - The atomic value to convert,
* exponential e.g 1e+4 will result in 14, only numbers are taken, negative sign is ignored.
* @param {number} options.decimals - The number of decimal places (positive), it will round down (BigNumber.ROUND_DOWN).
*
* @returns {BigNumber} The decimal representation from the atomic value.
*/
function atomicToDecimal(_ref) {
let {
value,
decimals
} = _ref;
const matches = value.toString().match(pattern);
if (!matches) return new _bignumber.BigNumber(0);
const combinedNumberString = matches.join('');
const bn = new _bignumber.BigNumber(combinedNumberString);
return bn.shiftedBy(-decimals).decimalPlaces(Math.abs(decimals), _bignumber.BigNumber.ROUND_DOWN);
}
//# sourceMappingURL=atomic-to-decimal.js.map