SPB Git forge

spb/spinza

Public
8commits 1branches 0releases
1.6 MBsize
maindefault branch
16 days agolast push
TypeScript 97.6% SQL 1.4% JavaScript 0.5%
1.1 KB · 26 lines typescript
Raw Blame History
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