2

Using JavaScript, what is correct approach to replace the dot to comma (For EU currency), for example:

2000.65 would be 2000,65 instead of 2,000.65

39.20 would be 39,20

I am not sure if cost.replace('.', ',') is the right way. Is there a better option?

user88432
  • 397
  • 1
  • 4
  • 13
  • Check this answer: https://stackoverflow.com/questions/17320162/how-to-replace-comma-with-a-dot-in-the-number-or-any-replacement You have a duplicated question. – dpapadopoulos Feb 18 '19 at 10:09
  • 2
    Sounds like a localization problem. There might be js native solutions. – Zim84 Feb 18 '19 at 10:09
  • http://openexchangerates.github.io/accounting.js/ – Quentin Feb 18 '19 at 10:10
  • Possible duplicate of [How can I format numbers as dollars currency string in JavaScript?](https://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-dollars-currency-string-in-javascript) – Pete Feb 18 '19 at 10:10

2 Answers2

8

You can use Intl.NumberFormat

const n = 2000.65;

console.log(new Intl.NumberFormat('de-DE', {
  style: 'currency',
  currency: 'EUR'
}).format(n));

There are some more options available, like showing the thousand seperator or not, or displaying the sign or not. Below will only display the number in european notation, without thousands separator.

const n = 2000.65;

console.log(new Intl.NumberFormat('de-DE', {
   useGrouping: false,
}).format(n));
baao
  • 71,625
  • 17
  • 143
  • 203
4

Try using toLocaleString()

Example:

    var d = 1000000.54;
    var n = d.toLocaleString(); // output would be 1,000,000.54
    console.log(n);