Todae

Todae

formatNumber and parseNumber

formatNumber(value, { locale, formatOptions }) writes a number as Field.NumberInput does, such as 1,234.5 in en and 1.234,5 in de, and parseNumber(text, { locale, formatOptions }) reads text back into a number, or returns null when the text is not one. formatOptions takes what Intl.NumberFormat takes, so a percent, a currency or a unit is a style away. Without locale, both use the runtime's own. Use them to show or read the same number outside the input.

On this page
import { Code, DataList, Field, formatNumber, Label, parseNumber, Stack } from '@tounsoo/todae';
import { useId, useState } from 'react';

// What parseNumber reads from the text, and how formatNumber writes it back.
export default function NumberRoundTrip({ locale }: { locale: string }) {
  const id = useId();
  const [text, setText] = useState('1234.5');
  const value = parseNumber(text, { locale });
  return (
    <Stack.Root gap="condensed">
      <Field.Group>
        <Label.Root htmlFor={id}>Amount</Label.Root>
        <Field.Root id={id}>
          <Field.TextInput value={text} onChange={(event) => setText(event.target.value)} />
        </Field.Root>
      </Field.Group>
      <DataList.Root aria-label="Round trip">
        <DataList.Item>
          <DataList.Term>parseNumber</DataList.Term>
          <DataList.Details>
            <Code.Root>{value === null ? 'null' : String(value)}</Code.Root>
          </DataList.Details>
        </DataList.Item>
        <DataList.Item>
          <DataList.Term>formatNumber</DataList.Term>
          <DataList.Details>
            <Code.Root>{value === null ? 'null' : formatNumber(value, { locale })}</Code.Root>
          </DataList.Details>
        </DataList.Item>
      </DataList.Root>
    </Stack.Root>
  );
}
parseNumber
1234.5
formatNumber
1,234.5
formatNumber(0.125, { locale: 'de', formatOptions: { style: 'percent', maximumFractionDigits: 1 } }); // "12,5\u00a0%"
parseNumber('12,5 %', { locale: 'de', formatOptions: { style: 'percent' } }); // 0.125

What parseNumber reads

parseNumber learns the locale's separators, minus sign and digits from Intl.NumberFormat, so 1.234,5 in de, 1 234,5 with a narrow no-break space in fr and ١٬٢٣٤٫٥ in ar-EG all read as 1234.5, and full-width digits fold to ASCII. It accepts the text with or without grouping, but a group separator only where the locale puts one, so 1.234 in de is 1234, and 1,5 in en is no number. A period is a decimal point in a locale that doesn't group with one, such as fr.

It reads the percent sign, the currency sign or the unit that formatOptions writes, or its absence. It doesn't read compact or scientific notation, so formatOptions leaves notation and compactDisplay out. It also leaves out maximumSignificantDigits, minimumSignificantDigits, roundingMode, roundingIncrement and roundingPriority, which round the text differently from a Field.NumberInput commit. TypeScript rejects all of these: use fraction digits. A percent style takes the number as a fraction: formatNumber writes 0.125 as 13%, or 12.5% with maximumFractionDigits: 1, and parseNumber reads 13% as 0.13.

Accessible composition

Todae handles: Text in the locale you pass, or the runtime's own when you pass none.

You supply: A locale that matches the page's, and the same locale and formatOptions for writing and reading. A message near the input when parseNumber returns null.

Watch for: On a server with no locale, the text uses the server's locale, so a mismatch can make hydration warn.

When to use

formatNumber

When: Writing a number as text for a locale, such as in a table cell next to a number input.

When not: A number in a style formatOptions can't write, such as compact notation → Intl.NumberFormat.

parseNumber

When: Reading a number someone typed, in the locale and format the input uses.

When not: Text in a fixed format such as 1234.5 → Number.

Props

formatNumber props
NameTypeDescription
value Requirednumber

The number to write.

options.localestring

The locale, such as de. The runtime's own when not set.

options.formatOptionsNumberFormatOptions

How the number is written, as Intl.NumberFormat takes it: style, such as percent or currency, and fraction digits.

parseNumber props
NameTypeDescription
text Requiredstring

The typed text.

options.localestring

The locale, such as de. The runtime's own when not set.

options.formatOptionsNumberFormatOptions

How the number is written, as Intl.NumberFormat takes it. A percent style reads 13 as 0.13.