1

I am trying to format a given number to European format. However, I am getting result in US/UK format only.

Here is my code:

static async formatCurrency(numberIn: number, currencyIndicator: string) {
        if(currencyIndicator=='EUR') {
            console.log(numberIn.toLocaleString('es-ES', { minimumFractionDigits: 2 , style: 'currency', currency: 'EUR'}));
            console.log(Number(numberIn).toLocaleString("de-DE", {minimumFractionDigits: 2}));
        }
}
await formatCurrency(12345678.00,"EUR");

Here is my output:

€12,345,678.00

12,345,678.00

The output I am expecting is:

€12.345.678,00

12.345.678,00

Zeus
  • 166
  • 1
  • 2
  • 14
  • Is this in the browser or NodeJS? And if browser, which browser are you testing with? – Lionel Rowe Sep 16 '20 at 15:38
  • This is currently in NodeJS. But I will be running my automation scripts in Chrome, Edge, Firefox and Safari. If I copy same code in Chrome's console then I am getting expected result. But when I run the script in Visual Studio Code I get the result mentioned in the question. – Zeus Sep 16 '20 at 15:43
  • I think it's due to [this](https://stackoverflow.com/a/39626602). But in Chrome, `es-ES` and `de-DE` locales both place the € sign _after_ the number (`nl-NL` locale would work for what you want, but with a non-breaking space between the sign and the number). Also, you don't need to `await` synchronous functions like `console.log`. – Lionel Rowe Sep 16 '20 at 15:48
  • Does this answer your question? [Where does Intl.NumberFormat support come from in node.js?](https://stackoverflow.com/questions/39626119/where-does-intl-numberformat-support-come-from-in-node-js) – Joachim Sauer Sep 16 '20 at 16:10
  • why on earth do you have async function and "await" on console log? whats going on here, please fix it, it hurts my eyes! – Talg123 Sep 16 '20 at 16:16
  • I actually had return statement there which I replaced with console log temporarily. But forgot to remove await. – Zeus Sep 16 '20 at 16:37

1 Answers1

1

I tried installing full-icu and importing it to my class. With this code I am still not getting desired result:

            formattedNumber = await new Intl.NumberFormat('nl-NL', { minimumFractionDigits: 2 , style: 'currency', currency: 'EUR'}).format(numberIn);
           
 console.log("formattedNumber is: " + formattedNumber);

This is what I get as output: €12,345,678.00. Instead of €12.345.678,00

May be this will be better solution?

            formattedNumber = await formattedNumber.replace(/\./g, "_");
            formattedNumber = await formattedNumber.replace(/,/g, ".");
            formattedNumber = await formattedNumber.replace(/_/g, ",");
    ```
Zeus
  • 166
  • 1
  • 2
  • 14