Format number
Shows a number, price, percentage, measurement or file size the way readers in their language expect it.
Example
Downloads:
Price:
Conversion:
Speed:
File:
Total :
<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>import { FormatNumber } from '@focsd-dev/react';
export function Example() { return <FormatNumber value="1234567" notation="compact"></FormatNumber>;}<script setup>import '@focsd-dev/components/focsd-format-number.js';</script>
<template> <focsd-format-number value="1234567" notation="compact"></focsd-format-number></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-format-number.js';
// template<focsd-format-number value="1234567" notation="compact"></focsd-format-number><script> import '@focsd-dev/components/focsd-format-number.js';</script>
<focsd-format-number value="1234567" notation="compact"></focsd-format-number>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
value | value | number | 0 |
type | type | 'decimal' | 'currency' | 'percent' | 'unit' | 'bytes' | decimal |
currency | currency | string | — |
unit | unit | string | — |
notation | notation | 'standard' | 'scientific' | 'engineering' | 'compact' | standard |
minimum-fraction-digits | minimumFractionDigits | number | undefined | — |
maximum-fraction-digits | maximumFractionDigits | number | undefined | — |
locale | locale | string | — |
binary | binary | boolean | false |
CSS parts
Style the inside of the component with ::part(), for example focsd-format-number::part(data).
| Part | Description |
|---|---|
data | The `<data>` element holding the formatted text. |
Accessibility
- The text sits in a
<data>element whosevalueis the raw number, so scripts and machines can read the exact figure. - Without
localeit follows the nearestlangattribute, so the separators match the language around it and screen readers read it in that language. - Write the unit in
unitorcurrencyrather 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 withbinary), sonotation="compact"changes nothing andnotation="scientific"gives text such as "1.5E0 KiB". Leavenotationatstandardfor sizes.
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.