Formatting Numbers
toFixed rounds to a number of decimals and returns a string. toLocaleString formats with separators and currency. Intl is the full version.
Example
const n = 1234567.891;
console.log(n.toFixed(2), n.toLocaleString("en-US"));
console.log((0.5).toLocaleString("en-US", { style: "percent" }));
console.log((1234.5).toLocaleString("en-US", { style: "currency", currency: "USD" }));Exercise
Two decimals.
n.toFixed()
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.