UNPKG

@melonproject/token-math

Version:

A small helper library to do precision safe calculations

19 lines (18 loc) 826 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const toString_1 = __importDefault(require("../bigInteger/toString")); const toFixed = (quantity, decimals = 6) => { const minDecimals = Math.min(decimals, quantity.token.decimals); const string = toString_1.default(quantity.quantity).padStart(quantity.token.decimals); const before = string.slice(0, -quantity.token.decimals).trim(); const after = string .slice(-quantity.token.decimals) .slice(0, minDecimals) .replace(/ /gi, "0"); const withDecimal = `${before || "0"}${after.length > 0 ? "." : ""}${after}`; return withDecimal; }; exports.default = toFixed;