MoreLesson 237Free preview
Format a Number
This lesson is part of How To · All levels
Summary
This lesson shows you how to display numbers in neat format in JavaScript. You can round the decimal with toFixed(), add the denominator of thousands, and format the currency following locale using Intl.NumberFormat. Note that the resulting formatting is usually a string, not a number.
Key points
- ToFixed(n) round the numbers and return the string with n decimal digits.
- Intl.NumberFormat follows locale rules, for example 'id-ID' using the thousand dots and decimal commas.
- Option style: 'currency' together with currency: 'IDR' formats value as currency.
- Number() or parseFloat() are required if the resulting format string is to be recalculated.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.