SPB Git

spb/spinza

Public
1.1 KB · 26 lines typescript
1import { CURRENCY_SHORT } from "./constants";23/** Format fictional credits: 10000 -> "10,000 SC". Never uses a currency sign. */4export function formatSC(amount: number | bigint | string, opts: { unit?: boolean } = {}): string {5  const n = typeof amount === "bigint" ? Number(amount) : typeof amount === "string" ? Number(amount) : amount;6  const s = Math.trunc(n).toLocaleString("en-US");7  return opts.unit === false ? s : `${s} ${CURRENCY_SHORT}`;8}910/** Compact: 1250000 -> "1.25M" */11export function formatCompact(n: number): string {12  if (Math.abs(n) >= 1_000_000) return `${(n / 1_000_000).toFixed(2).replace(/\.?0+$/, "")}M`;13  if (Math.abs(n) >= 10_000) return `${(n / 1_000).toFixed(1).replace(/\.?0+$/, "")}K`;14  return Math.trunc(n).toLocaleString("en-US");15}1617export function formatMultiplier(m: number): string {18  if (m >= 100) return `${Math.round(m).toLocaleString("en-US")}×`;19  if (m >= 10) return `${m.toFixed(1).replace(/\.0$/, "")}×`;20  return `${m.toFixed(2).replace(/\.?0+$/, "")}×`;21}2223export function formatPercent(p: number, digits = 2): string {24  return `${(p * 100).toFixed(digits)}%`;25}26
Files · ed7124be1f00e0dd196bd0586d2e839d04b0f72c
Menu

Theme