Skip to content
Focsd homeFocsd homeFocsd
Forms

Format number

Shows a number, price, percentage, measurement or file size the way readers in their language expect it.

  • Tag: focsd-format-number
  • Pattern: Data element (Intl.NumberFormat)
  • Category: Utility

Example

Downloads:

Price:

Conversion:

Speed:

File: , disk image:

Total :

HTML
<p>
Downloads:
<focsd-format-number value="1234567" notation="compact"></focsd-format-number>
</p>
<p>
Price:
<focsd-format-number
value="1234.5"
type="currency"
currency="EUR"
locale="de"></focsd-format-number>
</p>
<p>
Conversion: <focsd-format-number value="0.256" type="percent"></focsd-format-number>
</p>
<p>
Speed:
<focsd-format-number
value="50"
type="unit"
unit="kilometer-per-hour"></focsd-format-number>
</p>
<p>
File: <focsd-format-number value="1500" type="bytes"></focsd-format-number>, disk image:
<focsd-format-number value="1536" type="bytes" binary></focsd-format-number>
</p>
<p lang="fr">Total : <focsd-format-number value="1234.5"></focsd-format-number></p>

Usage

Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.

<script type="module">
import '@focsd-dev/components/focsd-format-number.js';
</script>
<focsd-format-number value="1234567" notation="compact"></focsd-format-number>

API

Attributes

AttributePropertyTypeDefault
valuevaluenumber0
typetype'decimal' | 'currency' | 'percent' | 'unit' | 'bytes'decimal
currencycurrencystring—
unitunitstring—
notationnotation'standard' | 'scientific' | 'engineering' | 'compact'standard
minimum-fraction-digitsminimumFractionDigitsnumber | undefined—
maximum-fraction-digitsmaximumFractionDigitsnumber | undefined—
localelocalestring—
binarybinarybooleanfalse

CSS parts

Style the inside of the component with ::part(), for example focsd-format-number::part(data).

PartDescription
dataThe `<data>` element holding the formatted text.

Accessibility

  • The text sits in a <data> element whose value is the raw number, so scripts and machines can read the exact figure.
  • Without locale it follows the nearest lang attribute, so the separators match the language around it and screen readers read it in that language.
  • Write the unit in unit or currency rather than next to the number, so it is formatted and read together with it.
  • It is plain inline text: it takes the surrounding font and color, and adds no role or focus stop.
  • A value that is not a number shows nothing (and warns in the console), rather than "NaN".
  • With type="bytes" the size is scaled to its unit first (kB, or KiB with binary), so notation="compact" changes nothing and notation="scientific" gives text such as "1.5E0 KiB". Leave notation at standard for sizes.