1

I have some strange example. If I write this:

parseFloat("12345,987").toLocaleString("slv")

the result would be as expected(slv has comma as decimal separator): 12.345,987

But if I round first to 2 decimals:

parseFloat("12345,987").toFixed(2).toLocaleString("slv")

the result would be always with english separator(dot): 12,345.99

How is that possible? It should be: 12.345,99

artm
  • 8,554
  • 3
  • 26
  • 43
Simon
  • 1,955
  • 5
  • 35
  • 49

2 Answers2

4

As described in this answer, parseFloat() is meant to be used with decimals only and therefore passing in a string with commas can lead to unexpected results.

For example, I tried parseFloat("12345,987").toLocaleString("slv") in JsFiddle and my browser (UK locale) gave me 12.345, which is different to your result.

One other point to note is that toFixed() returns a string but toLocaleString() operates on numbers. Applying toLocaleString() to a string might also produce unexpected results for a different reason.

Community
  • 1
  • 1
djskinner
  • 8,035
  • 4
  • 49
  • 72
2

Thank you. I have found a solution: parseFloat("12345,987").toLocaleString("slv", { minimumFractionDigits: 2, maximumFractionDigits: 2 });

This works ok. "For example, I tried parseFloat("12345,987").toLocaleString("slv")"

"djskinner" - Can you try with: "sl-SI" instead of slv? Does it still doesn't work?

Simon
  • 1,955
  • 5
  • 35
  • 49