Chapter 12 · Beyond the DOM
Formatting numbers, money and dates
Computers store plain values like 149999.5 and 2026-11-29, but people want to read ₹1,49,999.50 and 29 Nov 2026. In this chapter you'll turn raw values into friendly text: prices, big numbers, percentages, "3 students", dates, and "5 minutes ago".
Why formatting matters
The same amount is written differently around the world. India groups digits in lakhs and crores; most other countries group in thousands, and some use a comma for the decimal point:
1,49,999.5149,999.5149.999,5149 999,5Writing all those rules yourself would be a lot of work. Your visitor's browser already knows them, and Nyx gives you short, readable ways to use them.
Do your maths with plain numbers, and format only when you show the result. Formatted text like "₹1,49,999.50" is for people to read; JavaScript can't add it up.
Money: nyx.money
Give nyx.money() an amount and a currency code:
nyx.money(show as money
149999,the amount
"INR"which currency
)Done
Watch the Output below.
Each currency is written the way its own country writes it: euros with a comma for the decimal point, and Japanese yen without decimals, because yen has no smaller coins.
Currencies use three-letter codes. The ones you'll meet most:
"INR"Indian rupee (₹)"USD"US dollar ($)"EUR"Euro (€)"GBP"British pound (£)"JPY"Japanese yen (¥)"AUD", "CAD"Australian and Canadian dollars"AED"UAE dirham"SGD"Singapore dollar"BDT", "NPR", "LKR"Bangladeshi taka, Nepalese and Sri Lankan rupeesEvery currency in the world has a code like this. If you write "Rs" or "₹", or make a typo like "IRN", Nyx tells you the right code.
Without the paise
Shops often show whole rupees. Add { decimals: 0 } to choose how many decimals to show. The amount is rounded:
Watch the Output below.
Prices from an input
As you saw in Changing content, .value() always gives you text, even from a number box. nyx.money() accepts that text, but to do maths you first turn it into a number with JavaScript's Number(). Change the price or the quantity:
Total:
Text
"349" + "2"Gives "3492": + joins text together.
Numbers
Number("349") + Number("2")Gives 351.
Empty the price box and the total becomes ₹0.00, because Number("") turns empty text into 0. Text that isn't a number, like "abc", makes Number() give NaN ("not a number"), and Nyx then tells you that nyx.money() needs a number.
Numbers: nyx.number
nyx.number() adds the separators that make big numbers readable. Add a style for short numbers or percentages:
nyx.number(1234567)12,34,567nyx.number(2500000, "short")25L; in the US, 2.5Mnyx.number(0.256, "percent")25.6%Watch the Output below.
0.75 means 75%. So divide first: nyx.number(45 / 60, "percent"). If you write nyx.number(75, "percent"), you'll get 7,500%.
"1 student", "3 students": nyx.plural
"1 students" and "3 student" look careless, and it's easy to get wrong. nyx.plural() puts the count and the right word together. Click the buttons:
Nyx follows the usual English rules. For words that don't follow them, give the plural yourself:
nyx.plural(3, "student")3 studentsnyx.plural(1, "student")1 studentnyx.plural(0, "class")0 classes. Words ending in s, x, z, ch or sh add "es".nyx.plural(2, "city")2 cities. A consonant and "y" becomes "ies".nyx.plural(40, "child", "children")40 children. Irregular words: give the plural too.Giving Nyx a date
The date tools accept a date in any of these forms:
new Date()Plain JavaScript for "right now".Date.now()Also "right now", as a number of milliseconds. Date.now() - nyx.time("3 hours") is three hours ago."2026-11-29"A day, written year-month-day. This is also what <input type="date"> gives you."2026-11-29T15:45"A day and a time, with a T between them. Servers often send dates this way, like WordPress's post.date.In India that's 29 November, but in the US the month comes first, so 03/04/2026 is 4 March in one country and 3 April in another. Computers only trust year-month-day, so Nyx refuses the others and tells you how to write them.
Dates and times: nyx.date
nyx.date() shows a date the way the visitor's country writes it. Choose how much to show with a style:
nyx.date(d)29 Nov 2026nyx.date(d, "short")29/11/26nyx.date(d, "long")Sunday, 29 November 2026nyx.date(d, "time")3:45 pmnyx.date(d, "datetime")29 Nov 2026, 3:45 pmWatch the Output below.
A date picker gives you year-month-day text, which goes straight into nyx.date(). Pick a date:
"5 minutes ago": nyx.ago
For messages, comments and notifications, "5 minutes ago" is friendlier than a time. nyx.ago() works out how long ago a moment was, or how far in the future:
Watch the Output below.
Keeping it fresh
"Just now" stays "just now" unless you update it. Combine it with nyx.every() from Timers and delays. Each message keeps the moment it was sent with .data("sent", Date.now()), and the text is refreshed every 10 seconds:
Send a message, wait a little, and watch "just now" turn into "10 seconds ago", then "20 seconds ago".
Choosing a country style: nyx.locale
By default, numbers and dates follow the visitor's own settings, so a visitor in India and a visitor in the US can see the same page written differently. That's usually what you want, and it's why the examples on this page may look slightly different on your computer.
If your site is for one country, set nyx.locale once, at the top of your code, and everything follows it:
Watch the Output below.
A locale is a language code, a dash, and a country code. Some common ones:
"en-IN"English, India: 12,34,567, 29 Nov 2026"hi-IN"Hindi, India: 29 नव॰ 2026"bn-IN"Bengali, India, with Bengali digits: ১২,৩৪,৫৬৭"en-US"English, United States: 1,234,567, Nov 29, 2026"en-GB"English, United Kingdom: 1,234,567, 24-hour times"de-DE"German, Germany: 1.234.567"fr-FR"French, France: 1 234 567"ja-JP"Japanese, Japan: 2026/11/29Money is the exception: unless you set nyx.locale, each currency uses its home country's style, so euros always look European. To choose for one amount only: nyx.money(20, "EUR", { locale: "en-IN" }).
Putting it together
A school fee receipt. All the maths uses plain numbers, and everything is formatted only when it's shown:
Fee receipt
Total:
subtotal += fee.amount;Add up the plain numbers.+=means "add this to what's already there".subtotal - subtotal * discountTake 10% off, still with plain numbers.nyx.money(total, "INR")Format only now, when the total is shown.nyx.number(discount, "percent")0.1shows as10%.nyx.ago(dueOn)A date in the future reads "in 7 days".
Common mistakes
.value() gives text. Use Number(...) before adding.nyx.number(0.75, "percent")."Rs" or "₹"Use the three-letter code: "INR"."2026-11-29".nyx.every().nyx.locale if your site is for one country.Exercise
Fill in the blanks, then press Check answers.
1. Show 2500 as Indian rupees.
nyx.money(2500, "");
2. Show a price without decimals.
nyx.money(499, "INR", { : 0 });
3. Show 1500000 as a short number.
nyx.number(1500000, "");
4. Show 18 out of 20 as a percentage.
nyx.number(18 / 20, "");
5. Show "4 boxes".
nyx.(4, "box");
6. Show a message's time as "3 hours ago".
nyx.(message.sentAt);
7. Write 5 January 2027 the way computers trust.
nyx.date("");
Remember
nyx.money(amount, "INR") uses three-letter currency codes. { decimals: 0 } drops the paise..value() gives text. Use Number() before doing maths.nyx.number() adds separators; "short" gives 25L or 2.5M; "percent" expects a fraction.nyx.plural(3, "student") gives "3 students".nyx.date() shows them; nyx.ago() says how long ago.nyx.locale.