Number and Currency Formatting in JavaScript using Intl.NumberFormat - eviltoast
  • towerful@programming.dev
    link
    fedilink
    arrow-up
    6
    ·
    7 months ago

    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!

  • overcast5348@lemmy.world
    link
    fedilink
    arrow-up
    3
    ·
    7 months ago

    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.devOP
      link
      fedilink
      arrow-up
      1
      ·
      7 months ago

      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
        link
        fedilink
        arrow-up
        2
        ·
        7 months ago

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