@unowallet/token-price-widget
Version:
Uno Wallet Token Price Widget for displaying token price information and linking to uno wallet for swapping
52 lines (51 loc) • 1.63 kB
JavaScript
export const formatCurrency = (value, locale, minValue = 0.01) => {
if (isNaN(Number(value))) {
return "";
}
const numVal = Number(value);
if (numVal === 0) {
return (0).toLocaleString(locale, {
style: "currency",
currency: "USD",
});
}
if (numVal < minValue) {
return `< ${minValue.toLocaleString(locale, {
style: "currency",
currency: "USD",
})}`;
}
else if (numVal < 1) {
// For numbers less than 1, ensure exactly two significant digits
return numVal.toLocaleString(locale, {
minimumSignificantDigits: 2,
maximumSignificantDigits: 2,
style: "currency",
currency: "USD",
});
}
else {
// For numbers 1 or greater, round to two decimal places
return numVal.toLocaleString(locale, {
minimumFractionDigits: 2,
maximumFractionDigits: 2,
style: "currency",
currency: "USD",
});
}
};
export const formatPercentage = (percentage, noSign, language, maximumFractionDigits) => {
const value = noSign ? Math.abs(percentage) : percentage;
const localeOptions = maximumFractionDigits !== undefined
? {
minimumFractionDigits: 0,
maximumFractionDigits: maximumFractionDigits,
}
: {
minimumFractionDigits: 0,
maximumSignificantDigits: 3,
};
return value > 0 && value < 0.001
? "<0.001%"
: `${value.toLocaleString(language || "en-US", localeOptions)}%`;
};