Intl API: Number Formatting
The text presents two JavaScript functions for formatting numbers. One, `viewsFormatter`, formats large numbers with commas, and the other, `currencyFormatter`, formats numbers as currency with a dollar sign and thousands separator. Both functions use the `Intl.NumberFormat` to display numbers in a compact format, as shown in the console output.
generated by
gemma3:4b
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