Skip to main content
xriptxr

Number Format

Info:Content
21

A number formatted for its locale: decimal, currency, percent, compact, or a unit.

number currency money percent percentage compact unit locale intl format

Live demo

live · @xtyle/astro

NumberFormat

Formats

decimal 1,234,567.891
currency $1,299.50 1,300 Japanese yen
percent 42.4% from 0.4237, Intl semantics
compact 2.5M 18K
unit 512 kilobytes 21.5°C 88 km/h

Decimals and sign

3.14 3.00 3.1416 2026
+4.2% 0.0% 12 +12

Locale

The same value through locale , and through a lang on the element.

1.299,50 € 1 299,50 € ₹1,299.50 1.234.567,891

Tones and fallback

+8.1% -2.7% 41% $9.50 n/a

Tabular figures

tabular (default) $1,299.50 $84.25 $10,402.10 $7.00 $911.75
tabular=false $1,299.50 $84.25 $10,402.10 $7.00 $911.75

NumberFormat displays one number through Intl.NumberFormat, so the grouping, the decimal mark, the currency symbol, and the unit all follow the reader's locale rather than whatever the source wrote. format picks the shape: decimal (the default), currency (with an ISO 4217 currency), percent, compact (2.5M), or unit (any unit Intl sanctions, such as kilobyte, celsius, or kilometer-per-hour).

percent follows Intl's semantics, so 0.42 reads 42%; pass the fraction, not the percentage. decimals fixes the fraction digits, and min-decimals / max-decimals override it on either side. sign controls when a sign shows, which is what a delta wants (exceptZero). The locale comes from locale, then the nearest lang attribute up the tree, then the runtime default. Figures are tabular by default so a column of them aligns, and an optional tone inks the number for a gain or a loss. It exists as an element, not only the formatNumber helper, because a fill or a j5ml fragment can place a component but cannot call a function. An empty or non-numeric value renders fallback, and an invalid currency, unit, or locale warns once and falls back to plain decimal formatting rather than throwing. A fill receives the result twice: formatted, the whole string, and parts, the same output split by formatToParts into typed pieces (currency, minusSign, integer, fraction, unit, …), so a mod can style the currency symbol or the cents on their own. formatNumberParts returns those pieces outside the element.

When to use

How this component composes with the rest of the set.

Put figures in a Table column: tabular figures line the digits up without any extra styling.
Pair a value with a toned sign="exceptZero" percent for a delta, the shape Stat reads as a trend.
Set lang once on a section and every NumberFormat inside it formats for that locale.

Props

17 props, straight from the manifest.

PropTypeDefaultBindingsDescription
value number | string —
html svelte astro
The number to display. A string is parsed with Number; an empty or non-numeric value renders fallback.
format NumberFormatStyle
decimal percent currency compact unit
decimal
html svelte astro
The shape of the output: decimal, percent (Intl semantics, so 0.42 reads 42%), currency, compact (Intl's compact notation, 2.5M), or unit.
currency string USD
html svelte astro
An ISO 4217 currency code, read when format is currency.
currencyDisplay html: currency-display NumberCurrencyDisplay
symbol narrowSymbol code name
symbol
html svelte astro
How the currency is named: symbol ($), narrowSymbol, code (USD), or name (US dollars).
unit string —
html svelte astro
A unit identifier Intl sanctions (kilobyte, celsius, kilometer-per-hour), read when format is unit.
unitDisplay html: unit-display NumberUnitDisplay
short long narrow
short
html svelte astro
How the unit is written: short (512 kB), long (512 kilobytes), or narrow (512kB).
decimals number —
html svelte astro
Fixes both the minimum and maximum fraction digits, 0 to 20.
minDecimals html: min-decimals number —
html svelte astro
The minimum fraction digits. Wins over decimals.
maxDecimals html: max-decimals number —
html svelte astro
The maximum fraction digits. Wins over decimals.
sign NumberSign
auto always exceptZero never
auto
html svelte astro
When the sign shows: auto (negatives only), always, exceptZero (the delta reading), or never.
grouping boolean true
html svelte astro
Whether digit-group separators show. grouping="false" turns them off, for an ID or a year.
locale string —
html svelte astro
A BCP 47 locale tag. When absent, the nearest lang attribute up the tree, then the runtime default.
fallback string ""
html svelte astro
The text shown when value is empty or not a number.
tone NumberFormatTone
accent neutral danger success warn info accent-2 accent-3 accent-4 red orange yellow green blue purple brown pink cyan gray white black
—
html svelte astro
Inks the number in a semantic role or named hue, the way a toned Text is inked. Omit to inherit the surrounding color.
tabular boolean true
html svelte astro
Tabular figures, so a column of numbers aligns digit for digit. tabular="false" switches to proportional figures.
formatted
read-only
string —
html
Read-only: the display string as it renders right now, the formatted number or the fallback text.
static boolean false
astro
Astro only: render the formatted number server-side and never load the runtime to hydrate it.

Anatomy

The named parts that make up the component. Created from script it renders a shadow root, where a ::part() handle is reachable from your own stylesheet and the class beside it is internal. Upgraded over server-rendered markup it renders light DOM, where the class is the handle.

number

Success:::part(number) .xtyle-number-format

The formatted figure: a <data> element whose value carries the raw number and whose text is the formatted string.

--accent-vivid --neutral-vivid --danger-vivid --success-vivid --warn-vivid --info-vivid --accent-2-vivid --accent-3-vivid --accent-4-vivid --red-vivid --orange-vivid --yellow-vivid --green-vivid --blue-vivid --purple-vivid --brown-vivid --pink-vivid --cyan-vivid --gray-vivid --white-vivid --black-vivid

Tokens & coverage

What the component consumes, checked live against what the algorithm produces.

Success:fully covered 21/21 consumed tokens produced default register: 311 tokens

Live coverage check against the xtyle-default register (derive(xtyleDefault, { anchors }) → coverComponent(manifest, register)). Every token this component consumes must be a key the algorithm produces.

--accent-2-vivid --accent-3-vivid --accent-4-vivid --accent-vivid --black-vivid --blue-vivid --brown-vivid --cyan-vivid --danger-vivid --gray-vivid --green-vivid --info-vivid --neutral-vivid --orange-vivid --pink-vivid --purple-vivid --red-vivid --success-vivid --warn-vivid --white-vivid --yellow-vivid

Accessibility

Renders a <data> element: its value attribute carries the raw machine-readable number, and its text is what screen readers announce.
The formatted text is a real string in the DOM, not a pseudo-element, so it is read, selected, and copied as written.
A tone is color only; when a gain or a loss matters, let the sign carry it too (sign="exceptZero").

Code

Formats

Decimal, currency, percent, compact, a unit, and a de-DE locale override on the same currency value.

<xtyle-number-format value="1234567.891"></xtyle-number-format>
<xtyle-number-format value="1299.5" format="currency" currency="USD"></xtyle-number-format>
<xtyle-number-format value="0.4237" format="percent" decimals="1"></xtyle-number-format>
<xtyle-number-format value="2450000" format="compact"></xtyle-number-format>
<xtyle-number-format value="512" format="unit" unit="kilobyte" unit-display="long"></xtyle-number-format>
<xtyle-number-format value="1299.5" format="currency" currency="EUR" locale="de-DE"></xtyle-number-format>

Deltas and fallback

sign="exceptZero" and a tone for a change, and fallback for a missing value.

<xtyle-number-format value="0.042" format="percent" decimals="1" sign="exceptZero" tone="success"></xtyle-number-format>
<xtyle-number-format value="-0.018" format="percent" decimals="1" sign="exceptZero" tone="danger"></xtyle-number-format>
<xtyle-number-format value="" fallback="n/a"></xtyle-number-format>

Parts for a mod

A fill receives parts beside formatted, the same output split into typed pieces; formatNumberParts gives the same split outside the element.

<!-- a mod's number-format fill reads bindings.parts and wraps each piece, so the currency symbol and the cents style on their own -->
<xtyle-number-format value="1234.5" format="currency" currency="EUR"></xtyle-number-format>

<script type="module">
	import { formatNumberParts } from "@xtyle/core";
	const parts = formatNumberParts(1234.5, { format: "currency", currency: "EUR", locale: "en-US" });
	console.log(parts.map((part) => part.type).join(" "));
</script>