Skip to main content
Since Shoelace 2.0 Code stable

Format Number

sl-format-number

Formats a number using the specified locale and options.

Examples

Number Formatter Basics

Localization is handled by the browser’s Intl.NumberFormat API. No language packs are required.



<div class="format-number-overview">
  <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>
</div>

<script>
  const container = document.querySelector('.format-number-overview');
  const formatter = container.querySelector('sl-format-number');
  const input = container.querySelector('sl-input');

  input.addEventListener('sl-input', () => (formatter.value = input.value || 0));
</script>
div.format-number-overview
  sl-format-number value="1000"
  br
  br
  sl-input type="number" value="1000" label="Number to Format" style="max-width: 180px;"

javascript:
  const container = document.querySelector(.format-number-overview);
  const formatter = container.querySelector(sl-format-number);
  const input = container.querySelector(sl-input);

  input.addEventListener(sl-input, () => (formatter.value = input.value || 0));

Percentages

To get the value as a percent, set the type attribute to percent.





<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 />
<sl-format-number type="percent" value="1"></sl-format-number>
sl-format-number type="percent" value="0"
br
sl-format-number type="percent" value="0.25"
br
sl-format-number type="percent" value="0.50"
br
sl-format-number type="percent" value="0.75"
br
sl-format-number type="percent" value="1"

Localization

Use the lang attribute to set the number formatting locale.

English:
German:
Russian:
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 />
Russian: <sl-format-number value="2000" lang="ru" minimum-fraction-digits="2"></sl-format-number>
| English:
sl-format-number value="2000" lang="en" minimum-fraction-digits="2"
br
| German:
sl-format-number value="2000" lang="de" minimum-fraction-digits="2"
br
| Russian:
sl-format-number value="2000" lang="ru" minimum-fraction-digits="2"

Currency

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.





<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 />
<sl-format-number type="currency" currency="CNY" value="2000" lang="zh-cn"></sl-format-number>
sl-format-number type="currency" currency="USD" value="2000" lang="en-US"
br
sl-format-number type="currency" currency="GBP" value="2000" lang="en-GB"
br
sl-format-number type="currency" currency="EUR" value="2000" lang="de"
br
sl-format-number type="currency" currency="RUB" value="2000" lang="ru"
br
sl-format-number type="currency" currency="CNY" value="2000" lang="zh-cn"

Component Props

Property Default Details
value 0

number

The number to format.

type 'decimal'

'currency' | 'decimal' | 'percent'

The formatting style to use.

noGrouping
no-grouping
false

boolean

Turns off grouping separators.

currency 'USD'

string

The ISO 4217 currency code to use when formatting.

currencyDisplay
currency-display
'symbol'

'symbol' | 'narrowSymbol' | 'code' | 'name'

How to display the currency.

minimumIntegerDigits
minimum-integer-digits

number

The minimum number of integer digits to use. Possible values are 1–21.

minimumFractionDigits
minimum-fraction-digits

number

The minimum number of fraction digits to use. Possible values are 0–20.

maximumFractionDigits
maximum-fraction-digits

number

The maximum number of fraction digits to use. Possible values are 0–0.

minimumSignificantDigits
minimum-significant-digits

number

The minimum number of significant digits to use. Possible values are 1–21.

maximumSignificantDigits
maximum-significant-digits

number

The maximum number of significant digits to use,. Possible values are 1–21.

updateComplete A read-only promise that resolves when the component has finished updating.

Learn more about attributes and properties.