Numbers
Locale-aware number formatting and parsing, and step snapping.
Reach for them for number fields and steppers. The helpers are pure: no DOM at import time or in the call path, so they run on the server and in tests. Each result below is computed when the page is built, by running the call shown.
Import
import {
formatNumber,
parseNumber,
getNumberFormatter,
parseFormatOptions,
resolveLocale,
snapToStep,
isWithinRange,
isOnStep,
} from '@ecopages/radiant-ui/helpers';Format and parse
formatNumber returns '' for a value that is not finite. parseNumber returns null for empty input. It drops every character except digits, . and - before converting, so text with no digits gives 0, not null. Both take a locale and optional Intl.NumberFormat options; parseFormatOptions reads those options from a JSON string.
| Call | Result |
|---|---|
formatNumber(1234.5, 'de-DE') | "1.234,5" |
formatNumber(0.256, 'en-US', { style: 'percent' }) | "26%" |
parseNumber('1.234,5', 'de-DE') | 1234.5 |
parseNumber('', 'en-US') | null |
parseNumber('abc', 'en-US') | null |
parseFormatOptions('{"style":"percent"}') | {"style":"percent"} |
Steps
Snap a value to the nearest step within bounds, and check a value against them.
| Call | Result |
|---|---|
snapToStep(7, 0, 100, 5) | 5 |
isOnStep(15, 0, 5) | true |
isOnStep(16, 0, 5) | false |
isWithinRange(5, 0, 10) | true |