Files
riwa-ionic/src/utils/helper.ts

45 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export function formatBalance(amount: MaybeRefOrGetter<number | string>, locale: Intl.LocalesArgument = "en-US"): string {
let value = toValue(amount);
if (!value) {
value = 0;
}
if (typeof value === "string" && Number.isNaN(Number(value))) {
value = 0;
}
return value.toLocaleString(locale, { minimumFractionDigits: 0, maximumFractionDigits: 2 });
}
export function timeToLocal(originalTime: number) {
const d = new Date(originalTime * 1000);
return Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours(), d.getMinutes(), d.getSeconds(), d.getMilliseconds()) / 1000;
}
export function formatAmount(amount: MaybeRefOrGetter<number | string | null | undefined>): string {
const value = toValue(amount);
if (Number.isNaN(Number(value))) {
return "0";
}
const num = Number(value);
// 不超过1万原样显示
if (num < 10000) {
return String(num);
}
// 1亿以上显示为xx亿
if (num >= 100000000) {
const yi = (num / 100000000).toFixed(1);
return yi.endsWith(".0") ? `${Number.parseInt(yi)}亿` : `${yi}亿`;
}
// 1万到1亿显示为xx万
if (num >= 10000) {
const wan = (num / 10000).toFixed(1);
return wan.endsWith(".0") ? `${Number.parseInt(wan)}` : `${wan}`;
}
return String(num);
}