Skip to content

Number Formatter Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Show a number in the format of the locale. value sets the number, and the content of the element shows while there is no value.

No data

<p class="number-formatter-basic">
<sv-number-formatter value="1234.5">No data</sv-number-formatter>
</p>
<style>
.number-formatter-basic {
display: grid;
place-content: center;
margin: 0;
font-size: 2em;
}
</style>

Currency and units

Show a price or a measure with its symbol or unit name. format-style="currency" with currency="EUR" writes the euro sign in the place the locale puts it.

<p class="number-formatter-currency-and-units">
<sv-number-formatter currency="EUR" format-style="currency" value="42.5"></sv-number-formatter>
</p>
<style>
.number-formatter-currency-and-units {
display: grid;
place-content: center;
margin: 0;
font-size: 2em;
}
</style>

Notation and digits

Shorten a large number, like a follower count, or set how its digits and sign show. notation="compact" writes 1234567.891 as "1.2M".

<p class="number-formatter-notation-and-digits">
<sv-number-formatter notation="compact" value="1234567.891"></sv-number-formatter>
</p>
<style>
.number-formatter-notation-and-digits {
display: grid;
place-content: center;
margin: 0;
font-size: 2em;
}
</style>

Prefix and suffix

Put a label, a unit, or an icon next to the number, like a price per month or a trend arrow. The prefix and suffix slots hold the content before and after the formatted number.

Pro plan
 / month
Revenue
<dl class="number-formatter-slots">
<dt>Pro plan</dt>
<dd>
<sv-number-formatter currency="USD" format-style="currency" fraction-digits="0" value="19">
<span slot="suffix">&nbsp;/ month</span>
</sv-number-formatter>
</dd>
<dt>Revenue</dt>
<dd>
<sv-number-formatter
class="number-formatter-slots-trend"
format-style="percent"
sign-display="exceptZero"
value="0.124"
>
<!-- Arrow up -->
<svg aria-hidden="true" slot="prefix" viewBox="0 0 16 16">
<path d="M8 3 13 10H3Z" fill="currentcolor" />
</svg>
</sv-number-formatter>
</dd>
</dl>
<style>
.number-formatter-slots {
display: grid;
grid-template-columns: auto auto;
gap: 0.5em 1.5em;
place-content: center;
margin: 0;
dt {
color: var(--sv-gray-3);
}
dd {
margin: 0;
}
svg {
inline-size: 0.75em;
margin-inline-end: 0.25em;
}
}
.number-formatter-slots-trend {
color: var(--sv-success);
}
</style>