Numbers
Counts, decimals, percentages, money, sizes and token counts, each in the notation of the region. The examples are in an English region; a Dutch one swaps the separators.
ts
import { formatBytes, formatDecimal, formatMoney, formatNumber, formatNumeral, formatPercent, formatRounded, formatTokens, formatUsdSignificant } from '@adecore/ui/format';tsx
import { formatBytes, formatDecimal, formatMoney, formatNumber, formatPercent, formatTokens, formatUsdSignificant, useFormatLocale } from '@adecore/ui/format';
import { PreferencesBar } from '../shared/preferences-bar.tsx';
import { Values } from '../shared/values.tsx';
export default function NumbersDemo() {
useFormatLocale();
return (
<div className="flex w-full max-w-lg flex-col gap-4">
<PreferencesBar />
<Values
rows={[
['formatNumber(1234567.8)', formatNumber(1234567.8)],
['formatDecimal(1.25)', formatDecimal(1.25)],
['formatPercent(4.25)', formatPercent(4.25)],
['formatPercent(42.5)', formatPercent(42.5)],
["formatMoney(5480.96, 'USD')", formatMoney(5480.96, 'USD')],
["formatMoney(12, 'EUR', 0)", formatMoney(12, 'EUR', 0)],
['formatUsdSignificant(0.004213)', formatUsdSignificant(0.004213)],
['formatTokens(1234567)', formatTokens(1234567)],
['formatTokens(412300)', formatTokens(412300)],
['formatBytes(1536)', formatBytes(1536)],
['formatBytes(52428800, true)', formatBytes(52428800, true)]
]}
/>
</div>
);
}| Function | Writes |
|---|---|
formatNumber(value) | A rounded, grouped whole number: 1,234,568 or 1.234.568. |
formatDecimal(value) | The same with at most one decimal. |
formatRounded(value, decimals) | At most decimals places, trailing zeros dropped: 39, 1.95. |
formatNumeral(numeral) | A database numeral (number or text such as 9007199254740993 or -0.50) in the region's notation, keeping every digit and decimal. Other text comes back unchanged. |
formatPercent(value) | A value that is already a percent: one decimal under ten, whole above. 4.3%, 43%. |
formatMoney(value, currency, maximumFractionDigits?) | An amount with the narrow symbol: $ 5.480,96 in a Dutch region, not US$ 5.480,96. currency is an ISO code such as USD. |
formatUsdSignificant(value) | Dollars to three significant digits, for amounts from fractions of a cent to tens of dollars. |
formatTokens(value) | A compact token count: 1.2M, 412K, 640. |
formatBytes(bytes, whole?) | A file size in B, KB, MB, GB or TB: 1.5 KB, one decimal under ten. whole rounds. |
None of these converts. formatMoney never changes the currency, and formatBytes counts in steps of 1024.