number
Helpers for numeric form fields and locale-aware display. toNumberOrNull normalises an optional value, mergeFormatOptions composes Intl.NumberFormat options with a predictable precedence, and formatNumber / formatCurrency / formatPercent format values for a locale. toNumberOrNull and mergeFormatOptions are used by InputNumber.
toNumberOrNull(value)
function toNumberOrNull(value: number | null | undefined): number | nullNormalises a numeric field value: empty and non-finite inputs collapse to null, while 0 and every other finite number pass through unchanged.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | number | null | undefined | The raw field value. |
Return value
number | null — the original finite number, or null for null, undefined, NaN, and Infinity.
Example
import { toNumberOrNull } from '@myghf/ui'
toNumberOrNull(0) // → 0 (zero is kept, not treated as empty)
toNumberOrNull(2.5) // → 2.5
toNumberOrNull(null) // → null
toNumberOrNull(undefined) // → null
toNumberOrNull(Number.NaN) // → null
const amount = toNumberOrNull(rawInput) // null when the field is blank
const isValid = amount !== nullmergeFormatOptions(input)
function mergeFormatOptions(input: {
currency?: string
formatOptions?: Intl.NumberFormatOptions
integer?: boolean
}): Intl.NumberFormatOptionsComposes the Intl.NumberFormatOptions for a number field from three optional sources, with a fixed precedence: currency → formatOptions → integer. Later sources win, so an explicit formatOptions entry overrides the currency shorthand, and integer forces zero fraction digits regardless of what came before.
Parameters
| Parameter | Type | Description |
|---|---|---|
currency | string | A currency code such as 'USD'. Adds style: 'currency' and currency. |
formatOptions | Intl.NumberFormatOptions | Explicit Intl options; merged over the currency shorthand. |
integer | boolean | When true, sets maximumFractionDigits: 0 last. |
Return value
A fresh Intl.NumberFormatOptions object. Nothing is mutated, and omitted sources contribute no keys.
Example
import { mergeFormatOptions } from '@myghf/ui'
mergeFormatOptions({ currency: 'USD' })
// → { style: 'currency', currency: 'USD' }
// formatOptions overrides the shorthand:
mergeFormatOptions({ currency: 'USD', formatOptions: { maximumFractionDigits: 0 } })
// → { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }
// integer is applied last and wins over formatOptions:
mergeFormatOptions({ formatOptions: { maximumFractionDigits: 3 }, integer: true })
// → { maximumFractionDigits: 0 }
new Intl.NumberFormat('en-US', mergeFormatOptions({ currency: 'EUR' })).format(1234.5)
// → '€1,234.50'Pass the result straight to Intl.NumberFormat or to the :format-options prop of InputNumber.
formatNumber(value, locale?, options?)
function formatNumber(value: number, locale?: string, options?: Intl.NumberFormatOptions): stringFormats a number for a BCP-47 locale, passing options through to Intl.NumberFormat. If the locale is invalid it falls back to String(value) instead of throwing.
import { formatNumber } from '@myghf/ui'
formatNumber(1234.5, 'en-US') // → '1,234.5'
formatNumber(1234.5, 'de-DE') // → '1.234,5'
formatNumber(1234.5, 'en-US', { maximumFractionDigits: 0 }) // → '1,235'formatCurrency(value, currency, locale?, options?)
function formatCurrency(value: number, currency: string, locale?: string, options?: Intl.NumberFormatOptions): stringFormats a number as currency. options wins over the currency shorthand.
import { formatCurrency } from '@myghf/ui'
formatCurrency(1234.5, 'USD', 'en-US') // → '$1,234.50'
formatCurrency(1234.5, 'EGP', 'ar-EG') // → '١٬٢٣٤٫٥٠ ج.م.'formatPercent(value, locale?, options?)
function formatPercent(value: number, locale?: string, options?: Intl.NumberFormatOptions): stringFormats a ratio as a percentage (0.25 → 25%).
import { formatPercent } from '@myghf/ui'
formatPercent(0.25, 'en-US') // → '25%'