Active Foundation · Example
Currency amounts
Currency amountsClient
| EUR, en | |
| EUR, nl | |
| JPY (no minor unit) | |
| KWD (three decimals) | |
| Whole amounts | |
| A change, signed | |
| Exact decimal string | |
| The same value as a number | |
| No amount |
Full source examples/format/currency-amounts.tsx
// apps/docs/examples/format/currency-amounts.tsx'use client';
import {formatCurrency, toDecimalString} from '@workspace/active-format';import { Table, TableBody, TableCell, TableRow,} from '@workspace/active-ui/components/primitives/table';
const EXACT = '12345678901234567.89';
/** * The currency comes from the data, the notation from the presentation. * Decimals follow the currency's minor units unless the call overrides them. */export default function CurrencyAmounts() { const rows: [string, string][] = [ ['EUR, en', formatCurrency({amount: 1234.5, currency: 'EUR'}, 'en')], ['EUR, nl', formatCurrency({amount: 1234.5, currency: 'EUR'}, 'nl')], [ 'JPY (no minor unit)', formatCurrency({amount: 1234.5, currency: 'JPY'}, 'en'), ], [ 'KWD (three decimals)', formatCurrency({amount: 1234.5, currency: 'KWD'}, 'en'), ], [ 'Whole amounts', formatCurrency({amount: 1234.5, currency: 'EUR'}, 'en', {decimals: 0}), ], [ 'A change, signed', formatCurrency({amount: 12.5, currency: 'EUR'}, 'en', { sign: 'exceptZero', }), ], [ 'Exact decimal string', formatCurrency({amount: toDecimalString(EXACT), currency: 'EUR'}, 'en'), ], [ 'The same value as a number', formatCurrency({amount: Number(EXACT), currency: 'EUR'}, 'en'), ], [ 'No amount', formatCurrency({amount: null, currency: 'EUR'}, 'en') || '(empty string)', ], ]; return ( <Table> <TableBody> {rows.map(([label, text]) => ( <TableRow key={label}> <TableCell className="text-muted-foreground">{label}</TableCell> <TableCell className="font-mono"> <output suppressHydrationWarning>{text}</output> </TableCell> </TableRow> ))} </TableBody> </Table> );}