2020-11-09 14:38:30 +00:00
# Format Number
[component-header:sl-format-number]
2020-11-20 22:02:03 +00:00
Localization is handled by the browser's [`Intl.NumberFormat` API ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat ). No language packs are required.
2020-11-09 14:38:30 +00:00
```html preview
< div class = "format-number-overview" >
2022-03-02 15:10:41 +00:00
< sl-format-number value = "1000" > < / sl-format-number >
< br / > < br / >
< sl-input type = "number" value = "1000" label = "Number to Format" style = "max-width: 180px;" > < / sl-input >
2020-11-09 14:38:30 +00:00
< / div >
< script >
const container = document.querySelector('.format-number-overview');
const formatter = container.querySelector('sl-format-number');
const input = container.querySelector('sl-input');
2022-03-02 15:10:41 +00:00
input.addEventListener('sl-input', () => (formatter.value = input.value || 0));
2020-11-09 14:38:30 +00:00
< / script >
```
2021-11-04 22:12:47 +00:00
```jsx react
import { useState } from 'react';
import { SlFormatNumber, SlInput } from '@shoelace-style/shoelace/dist/react';
const App = () => {
const [value, setValue] = useState(1000);
return (
2021-11-05 14:06:06 +00:00
< >
2021-11-04 22:12:47 +00:00
< SlFormatNumber value = {value} / >
2022-03-02 15:10:41 +00:00
< br / >
< br / >
< SlInput
type="number"
value={value}
label="Number to Format"
2021-11-04 22:12:47 +00:00
style={{ maxWidth: '180px' }}
onSlInput={event => setValue(event.target.value)}
/>
2021-11-05 14:06:06 +00:00
< />
2021-11-04 22:12:47 +00:00
);
};
```
2020-11-09 14:38:30 +00:00
## Examples
### Percentages
To get the value as a percent, set the `type` attribute to `percent` .
```html preview
2022-03-02 15:10:41 +00:00
< sl-format-number type = "percent" value = "0" > < / sl-format-number > < br / >
< sl-format-number type = "percent" value = "0.25" > < / sl-format-number > < br / >
< sl-format-number type = "percent" value = "0.50" > < / sl-format-number > < br / >
< sl-format-number type = "percent" value = "0.75" > < / sl-format-number > < br / >
2020-11-09 14:38:30 +00:00
< sl-format-number type = "percent" value = "1" > < / sl-format-number >
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlFormatNumber } from '@shoelace-style/shoelace/dist/react';
const App = () => (
< >
2022-03-02 15:10:41 +00:00
< SlFormatNumber type = "percent" value = {0} / >
< br / >
< SlFormatNumber type = "percent" value = {0.25} / >
< br / >
< SlFormatNumber type = "percent" value = {0.5} / >
< br / >
< SlFormatNumber type = "percent" value = {0.75} / >
< br / >
< SlFormatNumber type = "percent" value = {1} / >
2021-11-04 22:12:47 +00:00
< />
);
```
2020-11-09 14:38:30 +00:00
### Localization
2021-12-02 15:29:04 +00:00
Use the `lang` attribute to set the number formatting locale.
2020-11-09 14:38:30 +00:00
```html preview
2022-03-02 15:10:41 +00:00
English: < sl-format-number value = "2000" lang = "en" minimum-fraction-digits = "2" > < / sl-format-number > < br / >
German: < sl-format-number value = "2000" lang = "de" minimum-fraction-digits = "2" > < / sl-format-number > < br / >
2021-12-02 15:29:04 +00:00
Russian: < sl-format-number value = "2000" lang = "ru" minimum-fraction-digits = "2" > < / sl-format-number >
2020-11-09 14:38:30 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlFormatNumber } from '@shoelace-style/shoelace/dist/react';
const App = () => (
< >
2022-03-02 15:10:41 +00:00
English: < SlFormatNumber value = "2000" lang = "en" minimum-fraction-digits = "2" / >
< br / >
German: < SlFormatNumber value = "2000" lang = "de" minimum-fraction-digits = "2" / >
< br / >
2021-12-02 15:29:04 +00:00
Russian: < SlFormatNumber value = "2000" lang = "ru" minimum-fraction-digits = "2" / >
2021-11-04 22:12:47 +00:00
< />
);
```
2020-11-09 14:38:30 +00:00
### Currency
2021-12-02 15:29:04 +00:00
To format a number as a monetary value, set the `type` attribute to `currency` and set the `currency` attribute to the desired ISO 4217 currency code. You should also specify `lang` to ensure the the number is formatted correctly for the target locale.
2020-11-09 14:38:30 +00:00
```html preview
2022-03-02 15:10:41 +00:00
< sl-format-number type = "currency" currency = "USD" value = "2000" lang = "en-US" > < / sl-format-number > < br / >
< sl-format-number type = "currency" currency = "GBP" value = "2000" lang = "en-GB" > < / sl-format-number > < br / >
< sl-format-number type = "currency" currency = "EUR" value = "2000" lang = "de" > < / sl-format-number > < br / >
< sl-format-number type = "currency" currency = "RUB" value = "2000" lang = "ru" > < / sl-format-number > < br / >
2021-12-02 15:29:04 +00:00
< sl-format-number type = "currency" currency = "CNY" value = "2000" lang = "zh-cn" > < / sl-format-number >
2020-11-09 14:38:30 +00:00
```
2021-11-04 22:12:47 +00:00
```jsx react
import { SlFormatNumber } from '@shoelace-style/shoelace/dist/react';
const App = () => (
< >
2022-03-02 15:10:41 +00:00
< SlFormatNumber type = "currency" currency = "USD" value = "2000" lang = "en-US" / >
< br / >
< SlFormatNumber type = "currency" currency = "GBP" value = "2000" lang = "en-GB" / >
< br / >
< SlFormatNumber type = "currency" currency = "EUR" value = "2000" lang = "de" / >
< br / >
< SlFormatNumber type = "currency" currency = "RUB" value = "2000" lang = "ru" / >
< br / >
2021-12-02 15:29:04 +00:00
< SlFormatNumber type = "currency" currency = "CNY" value = "2000" lang = "zh-cn" / >
2021-11-04 22:12:47 +00:00
< />
);
```
2020-11-09 14:38:30 +00:00
[component-metadata:sl-format-number]