PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@iota/iota-sdk/dist/esm/utils
Просмотр файла: formatBalance.js
import BigNumber from "bignumber.js";
function formatAmountParts(amount) {
if (typeof amount === "undefined" || amount === null) {
return ["--"];
}
let postfix = "";
let bn = new BigNumber(amount.toString());
const bnAbs = bn.abs();
if (bnAbs.gte(1e9)) {
bn = bn.shiftedBy(-9);
postfix = "B";
} else if (bnAbs.gte(1e6)) {
bn = bn.shiftedBy(-6);
postfix = "M";
} else if (bnAbs.gte(1e4)) {
bn = bn.shiftedBy(-3);
postfix = "K";
}
if (bnAbs.gte(1)) {
bn = bn.decimalPlaces(2, BigNumber.ROUND_DOWN);
}
if (bnAbs.gt(0) && bnAbs.lt(1)) {
const leadingZeros = countDecimalLeadingZeros(bn.toFormat());
if (leadingZeros >= 4) {
return [formatWithSubscript(bn.toFormat(), leadingZeros), postfix];
} else {
return [bn.toFormat(leadingZeros + 1), postfix];
}
}
return [bn.toFormat(), postfix];
}
function formatAmount(...args) {
return formatAmountParts(...args).filter(Boolean).join(" ");
}
function countDecimalLeadingZeros(input) {
if (input === null) {
return 0;
}
const [, decimals] = input.toString().split(".");
if (!decimals) {
return 0;
}
let count = 0;
for (const digit of decimals) {
if (digit === "0") {
count++;
} else {
break;
}
}
return count;
}
const SUBSCRIPTS = ["\u2080", "\u2081", "\u2082", "\u2083", "\u2084", "\u2085", "\u2086", "\u2087", "\u2088", "\u2089"];
function formatWithSubscript(input, zeroCount) {
if (input === null) {
return "0";
}
const [, decimals] = input.toString().split(".");
const remainder = decimals.slice(zeroCount);
const digits = zeroCount.toString().split("");
const suscripts = digits.map((n) => SUBSCRIPTS[Number(n)]).join("");
return `0.0${suscripts}${remainder}`;
}
var CoinFormat = /* @__PURE__ */ ((CoinFormat2) => {
CoinFormat2["Rounded"] = "Rounded";
CoinFormat2["Full"] = "Full";
return CoinFormat2;
})(CoinFormat || {});
function formatBalance(balance, decimals, format = "Rounded" /* Rounded */, showSign = false) {
const bn = new BigNumber(balance.toString()).shiftedBy(-1 * decimals);
let formattedBalance = formatAmount(bn);
if (format === "Full" /* Full */) {
formattedBalance = bn.toFormat();
}
if (showSign && !formattedBalance.startsWith("-")) {
formattedBalance = `+${formattedBalance}`;
}
return formattedBalance;
}
export {
CoinFormat,
formatAmount,
formatBalance,
formatWithSubscript
};
//# sourceMappingURL=formatBalance.js.map
Выполнить команду
Для локальной разработки. Не используйте в интернете!