Intl API: Number Formatting
Fahim presents JavaScript code snippets for formatting numbers and currency using the `Intl.NumberFormat` object. The first snippet, `numbers-formatter.js`, formats a large number (1,722,012,999) into a compact notation with a prefix 'M', displaying as '1.7M'. The second snippet, `currency-formatter.js`, converts $5,210 into the compact currency format '$5.2K'.
generated by granite3.2:8b
The _
between numbers are numeric separators, although they are unnecessary, I find large numbers more readable this way.
Table of contents
Views Counter
const viewsFormatter = Intl.NumberFormat("en", { notation: "compact",});
const views = viewsFormatter.format(1_722_012_999);console.log(views); // 1.7M
Currency
const currencyFormatter = Intl.NumberFormat("en", { notation: "compact", style: "currency", currency: "USD",});
const amount = currencyFormatter.format(5_210);console.log(amount); // $5.2K