feat: y-axis SI prefix, hide tick from legend, fix table sort
This commit is contained in:
22
web/lib/formatNumber.ts
Normal file
22
web/lib/formatNumber.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
const SI_THRESHOLDS = [
|
||||
{ limit: 1_000_000_000, divisor: 1_000_000_000, suffix: 'G' },
|
||||
{ limit: 1_000_000, divisor: 1_000_000, suffix: 'M' },
|
||||
{ limit: 1_000, divisor: 1_000, suffix: 'K' },
|
||||
] as const;
|
||||
|
||||
export function formatSI(v: number, locale?: string): string {
|
||||
const abs = Math.abs(v);
|
||||
for (const { limit, divisor, suffix } of SI_THRESHOLDS) {
|
||||
if (abs >= limit) {
|
||||
const formatted = new Intl.NumberFormat(locale, {
|
||||
maximumFractionDigits: 3,
|
||||
minimumFractionDigits: 0,
|
||||
}).format(v / divisor);
|
||||
return `${formatted}${suffix}`;
|
||||
}
|
||||
}
|
||||
return new Intl.NumberFormat(locale, {
|
||||
maximumFractionDigits: 0,
|
||||
minimumFractionDigits: 0,
|
||||
}).format(v);
|
||||
}
|
||||
Reference in New Issue
Block a user