Number utils
import { formatCurrency, formatBytes, clamp } from '@gg-software/utils';All formatters use Intl.NumberFormat — pass locale in the options to override the
runtime default.
numberToString
Number → string, optionally in another base (2–36).
numberToString(42); // "42"
numberToString(255, 16); // "ff"formatNumber
Locale-aware formatting (thousands separators, decimals). Options are
Intl.NumberFormatOptions plus locale.
formatNumber(1234567.891); // "1,234,567.891" (runtime locale)
formatNumber(1234567.891, { locale: 'cs-CZ' }); // "1 234 567,891"
formatNumber(0.5, { maximumFractionDigits: 0 }); // "1"formatCurrency
Money formatting. currency is a required ISO 4217 code.
formatCurrency(1290, 'CZK', { locale: 'cs-CZ' }); // "1 290,00 Kč"
formatCurrency(19.9, 'EUR', { locale: 'de-DE' }); // "19,90 €"
formatCurrency(5, 'USD', { maximumFractionDigits: 0 }); // "$5"formatPercent
Percentage formatting — the input is a fraction (0–1).
formatPercent(0.42); // "42%"
formatPercent(0.4251, 1); // "42.5%"
formatPercent(0.42, 0, { locale: 'cs-CZ' }); // "42 %"| Parameter | Type | Default |
|---|---|---|
value | number (fraction) | — |
decimals | number | 0 |
options.locale | string | runtime locale |
formatBytes
Human-readable file sizes, base 1024 (B, KB, MB, GB, TB, PB).
formatBytes(1536); // "1.5 KB"
formatBytes(10_485_760); // "10 MB"
formatBytes(0); // "0 B"clamp
Constrain a value to the inclusive [min, max] range.
clamp(15, 0, 10); // 10
clamp(-2, 0, 10); // 0round
Round to N decimals without the usual floating-point drift.
round(1.005, 2); // 1.01 (naive Math.round gives 1.0)
round(1.2345, 3); // 1.235
round(1.6); // 2