<Num>
API Reference for the <Num> component
Overview
The <Num>
component renders a formatted number string in the user's locale, and can be customized with formatting options.
All reformatting is handled locally using the Intl.NumberFormat
library.
Reference
Props
Prop | Type | Default |
---|---|---|
children? | any | undefined |
name? | string | undefined |
value? | string | number | undefined |
options? | Intl.NumberFormatOptions | {} |
locales? | string[] | undefined |
Description
Prop | Description |
---|---|
children | The content to render inside the component. Typically a number, which will be formatted according to the current locale and options. If provided, it takes precedence over the value prop. |
name | Optional name for the number field, used for metadata purposes. |
value | The default value for the number. Can be a string or number. Strings will be parsed into numbers before formatting. |
options | Optional formatting options for the number, following the Intl.NumberFormatOptions specification. Use this to define styles such as currency, decimal precision, etc. |
locales | Optional locales to specify the formatting locale. If not provided, the default user's locale is used. Read more about specifying locales here. |
Returns
JSX.Element
containing the formatted number as a string.
Examples
Basic Example
In this example, item.quantity
will be reformatted according to the user's locale.
Specifying locales
By default, locales are determined by the user's browser settings,
but you can explicitly set the locale for the <Num>
component.
Translating <Num>
components
Let's say that you want your number to be in a larger sentence that gets translated.
Just add the <T>
components around the content.
Custom formatting
<Num>
uses the Intl.NumberFormat
library for formatting.
Dictionary examples
The <Num>
component can also be used in a dictionary entry.
Basic usage
Passing values to dictionary entries
If you want to pass values to the dictionary entry, you must specify a name for the <Num>
component and reference it when calling the t()
function.
Notes
- The
<Num>
component is used to format numbers according to a user's locale. - When inside of a
<T>
component, make sure to wrap all dynamic numbers in a<Num>
component.
Next steps
- For more details and usage examples of the
<Num>
component and other variable components like<Currency>
,<DateTime>
, and<Var>
, see the Using Variable Components documentation.