Number and Currency Formatting in JavaScript using Intl.NumberFormat

https://dev.to/schalkneethling/number-and-currency-formatting-in-javascript-using-intlnumberformat-46og

10 points · 5 comments · view on lemmy.world

5 Comments

towerful@programming.dev · 5 pts · 2y (1 reply)

Its worth creating an Intl formatter outside of a loop, then applying it inside the loop - if you are formatting everything in the loop the same.

const formatter = Intl.NumberFormat(/* options */);
return items.map(item => {
   return {
      ...item,
      total: formatter.format(item.total)
   }
});

...Or however you choose to mutate an array.

Well, certainly it used to be. Not sure if that is still the case!

schalkneethling@programming.dev · 1 pts · 2y

100% Agree

overcast5348@lemmy.world · 2 pts · 2y (2 replies)

I live in Canada and I've never seen "1.000,00". Canada (at least the anglophone part that I live in) follows the usual "1,000.00" format. Why is this library using commas as decimal separators?

schalkneethling@programming.dev · 1 pts · 2y (1 reply)

This is very interesting. I agree that some of the formatting puzzled me as well. I would assume this follows some standard even when the use of said standard is uncommon. I have for example not seen 1.000,00 € used, but I thought that perhaps if you live in Germany, France, or another EU country then it is common.

overcast5348@lemmy.world · 2 pts · 2y

Yup, that's indeed the format used in some European countries. Canada was surprising though. 🤷‍♂️