# Number Formatting ## Basic Usage You can localize the number with your definition formats. Number formats the below: ```js const numberFormats = { 'en-US': { currency: { style: 'currency', currency: 'USD', notation: 'standard' }, decimal: { style: 'decimal', minimumFractionDigits: 2, maximumFractionDigits: 2 }, percent: { style: 'percent', useGrouping: false } }, 'ja-JP': { currency: { style: 'currency', currency: 'JPY', useGrouping: true, currencyDisplay: 'symbol' }, decimal: { style: 'decimal', minimumSignificantDigits: 3, maximumSignificantDigits: 5 }, percent: { style: 'percent', useGrouping: false } } } ``` As the above, you can define named number formats (e.g. `currency`, etc), and you need to use [the options with ECMA-402 Intl.NumberFormat](https://tc39.es/ecma402/#numberformat-objects) After that, when using the locale messages, you need to specify the `numberFormats` option of `createI18n`: ```js const i18n = createI18n({ numberFormats }) ``` To localize Number value with Vue I18n, use the `$n` (via global injection) or `n` from `useI18n()`. > [!TIP] > `$n` has some overloads. About these overloads, see the [API Reference](../../api/vue/interfaces/ComponentCustomProperties.md#n) The following is an example of the use of `$n` in a template: ```html

{{ $n(10000, 'currency') }}

{{ $n(10000, 'currency', 'ja-JP') }}

{{ $n(10000, 'currency', 'ja-JP', { useGrouping: false }) }}

{{ $n(987654321, 'currency', { notation: 'compact' }) }}

{{ $n(0.99123, 'percent') }}

{{ $n(0.99123, 'percent', { minimumFractionDigits: 2 }) }}

{{ $n(12.11612345, 'decimal') }}

{{ $n(12145281111, 'decimal', 'ja-JP') }}

``` The first argument is numeric value as a parameter, and the second argument is number format name as a parameter. The last argument locale value as a parameter. As result the below: ```html

$10,000.00

¥10,000

¥10000

$988M

99%

99.12%

12.12

12,145,000,000

``` ## Usage in ` ``` For more advanced usage, see the [Composition API guide](../advanced/composition.md). ## Custom Formatting `$n` returns resulting string with fully formatted number, which can only be used as a whole. In situations when you need to style some part of the formatted number (like fraction digits), `$n` is not enough. In such cases NumberFormat component (`i18n-n`) will be of help. With a minimum set of properties, `i18n-n` generates the same output as `$n`, wrapped into configured DOM element. The following template: ```html ``` NumberFormat component has some props. The `tag` is the property to set the tag. The `value` is the property to set the numeric value to be formatted. The `format` is the property to which the format defined by the `numberFormats` option of `createI18n` can be set. The `locale` is the property to set the locale. It's localized with the locale specified by this prop instead of the one specified with the `locale` option of `createI18n`. Will produce the below output: ```html 100 $100.00 ¥100 ``` But the real power of this component comes into play when it is used with [scoped slots](https://v3.vuejs.org/guide/component-slots.html#scoped-slots). Let’s say there is a requirement to render the integer part of the number with a bolder font. This can be achieved by specifying `integer` scoped slot element: ```html ``` Template above will result in the following HTML: ```html $100.00 ``` It is possible to specify multiple scoped slots at the same time: ```html ``` (this resulting HTML was formatted for better readability) ```html € 1 , 234 . 00 ``` > [!NOTE] > Full list of the supported scoped slots as well as other `i18n-n`, properties can be found on [API Reference](../../api/general/variables/NumberFormat.md) ## Scope resolving The Scope resolving of NumberFormat component is same as Translation component. See the [here](../advanced/component.md#scope-resolving)