Overview
Numbers is a class and an attribute. nds-number-format formats the number in an element’s text, and can add a currency symbol or a unit. data-counter counts up to a number.
Pick another component when:
- the user types a number: Number Input
- the user picks a number in a range: Slider
Markup
Variants
Every option goes on the number, .nds-number-format. Counter makes it count up to the number it holds, and keeps its currency or unit. Start From and 2000 ms need Counter. A currency and a unit do not combine: use one.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Currency | None (default) | — | — | A number with no currency |
| Currency | SAR (id: sar) | [data-currency="SAR"] |
.nds-number-format:not([data-unit]) |
Saudi riyals. Draws the Riyal sign as an icon |
| Currency | USD | [data-currency="USD"] |
.nds-number-format:not([data-unit]) |
US dollars ($) |
| Currency | EUR | [data-currency="EUR"] |
.nds-number-format:not([data-unit]) |
Euros (€) |
| Currency | GBP | [data-currency="GBP"] |
.nds-number-format:not([data-unit]) |
Pounds sterling (£) |
| Currency | JPY | [data-currency="JPY"] |
.nds-number-format:not([data-unit]) |
Japanese yen (¥) |
| Currency | CNY | [data-currency="CNY"] |
.nds-number-format:not([data-unit]) |
Chinese yuan (¥) |
| Currency | INR | [data-currency="INR"] |
.nds-number-format:not([data-unit]) |
Indian rupees (₹) |
| Currency | KRW | [data-currency="KRW"] |
.nds-number-format:not([data-unit]) |
Korean won (₩) |
| Currency | TRY | [data-currency="TRY"] |
.nds-number-format:not([data-unit]) |
Turkish lira (₺) |
| Unit | Unit (hint: Any text after the number) | [data-unit="km"] |
.nds-number-format:not([data-currency]) |
A unit such as km, kg, MB or years. Not with a currency |
| Counter (any) | Counter (id: counter) (hint: Counts up when it scrolls into view) | [data-counter] |
.nds-number-format |
A headline figure that counts up once, to the number in its text. Combines with a currency or a unit. Use it for a few key figures, never for a value that keeps changing |
| Counter (any) | Start From (hint: Counts from 3,000,000, not from 0) | [data-counter-start="3000000"] |
[data-counter] |
For a large number: the count shows only its last part |
| Counter (any) | 2000 ms (hint: Slower than the 1000 ms default) | [data-counter-duration="2000"] |
[data-counter] |
A slower count, for a large number. Without it, a count takes one second |
Behavior
Counter Animation
A counter counts from data-counter-start to the data-counter value when half of it is in view. It runs once, and then the script sets data-animated on it. A user who asks the system for reduced motion sees the end value at once. The counter adds separators itself, so it needs nds-number-format only for a currency or a unit.
The counter reads data-counter when the count starts, not when the page loads. A value from a server works: set data-counter when the reply arrives, and a counter not yet in view counts to it with no other call. A counter that already ran keeps its old value: remove data-animated, then call NDS.Numbers.init().
Currency Symbol
data-currency adds the currency symbol beside the number. The symbol sits before the number in English and after it in Arabic. SAR draws the Riyal sign as an icon in the text color. The other codes add their Unicode symbol.
Unit
data-unit adds its text after the number, in English and in Arabic. CSS draws the unit, so it stays when a script changes the number, as the Slider does. After the change, call NDS.Numbers.format(el) to add the separators.
Built-in Features
-
Automatic Start
The loader starts Numbers on any page that has
nds-number-formatordata-counter. You write no script. -
Locale Separators
Separators follow the
langof the page, and digits are always Latin (0 to 9), whatever the language of the browser. Text before and after the number, the sign and the decimals stay. -
Child Elements Kept
Only the first text in the element that holds a digit changes. Icons and other child elements stay in place.
-
Safe to Run Again
The script remembers the text it wrote and reads the original number again. A number formatted twice reads the same as one formatted once, in every language.
-
Code Blocks Skipped
A number inside
<code>stays as written.
Best Practices
- Write the number in plain digits, with commas or no separators. It shows before the script runs. The script cannot read other separators, such as
3.240.000. - Write
0as a counter’s text. It shows until the count starts. - Write the number with the decimals you want shown.
1250.50shows as1,250.50, and a counter withdata-counter="98.6"counts with one decimal. - Use
data-currency, not a symbol in the text. See Currency Symbol. - A screen reader does not read the SAR icon. Where the currency matters, add it as hidden text after the number:
<span class="nds-number-format" data-currency="SAR">1250 <span class="nds-sr-only">riyals</span></span>. - Use
data-unitfor a unit on a number that a script changes. For a fixed suffix, write it in the text, such as98.5%. - Use a counter for a few headline figures, such as the statistics on a home page or KPI cards. A value loaded once from a server is fine. Do not use it for a value that keeps updating or that the user changes: it runs once.
- Keep
data-counter-durationbetween 800 and 2000 ms. A shorter count is too fast to see, and a longer one delays the figure. - For a free price, write the label, such as “Free” or “مجاني”, with no
data-currency. - Give a counter with a currency or a unit the
nds-number-formatclass too:<span class="nds-number-format" data-currency="SAR" data-counter="1250">0</span>. - Put a counter in a statistic card for a headline number with a label: a centered card, with the counter on its
nds-card-number.
API
Data Attributes
| Attribute | Element | Effect |
|---|---|---|
data-currency |
.nds-number-format |
Adds the currency symbol: SAR, USD, EUR, GBP, JPY, CNY, INR, KRW or TRY. Any other value adds nothing |
data-unit |
.nds-number-format |
Adds its text after the number as a unit. Do not combine it with data-currency |
data-counter |
any element | Makes the element a counter. Its value is the end value. Text before and after the number stays: $75,000, 98.6%, 1.5M. Empty, the counter counts to the number in its own text |
data-counter-start |
[data-counter] |
The start value. Default 0 |
data-counter-duration |
[data-counter] |
The length of the count in milliseconds. Default 1000 |
data-animated |
[data-counter] |
The script sets it to true when the count ends, and never removes it. A counter with it does not run. To run a counter again, remove it, then call NDS.Numbers.init(). The counter starts from data-counter-start again |
CSS Custom Properties
| Property | Default | Controls |
|---|---|---|
--number-icon-size |
1em |
The size of the SAR icon and of an .nds-icon child. Set it on .nds-number-format or on a parent |
JavaScript
The NDS.Numbers script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
| Method | Effect |
|---|---|
NDS.Numbers.init() |
Formats every number and gets every counter ready to count when it scrolls into view. The loader calls it. Call it after you add numbers or counters to the page |
NDS.Numbers.format(el) |
Formats one .nds-number-format element |
NDS.Numbers.formatAll() |
Formats every .nds-number-format on the page |
NDS.Numbers.initCounters() |
Gets every counter ready to count when it scrolls into view. It skips a counter with data-animated |
Numbers fires no events. To format a number in your own script, use NDS.formatNumber(n, options): it uses the lang of the page and Latin digits. The full API is in the banner of _js/nds-numbers.js.