UNPKG

@melonproject/token-math

Version:

A small helper library to do precision safe calculations

17 lines (16 loc) 757 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const BigInteger_1 = __importDefault(require("../bigInteger/BigInteger")); const appendDecimals = (token, n) => { const string = typeof n === "string" ? n : n.toString(); const [int, givenDecimals = ""] = string.split('.'); const padding = Array.apply(null, Array(Math.max(0, token.decimals - givenDecimals.length))) .map(() => 0) .join(""); const decimals = `${givenDecimals}${padding}`.slice(0, token.decimals); return new BigInteger_1.default(`${int}${decimals}`); }; exports.default = appendDecimals;