0

I have strings in currency format which i have to convert to float without using any js library.

2 079,15 EUR as an example should return 2079.15

Is there a simple way to do it ?

the solution proposed in How to convert a currency string to a double with jQuery or Javascript? is not working in this case:

As it will return 207915 in case of 2 079,15 EUR instead of 2079.15

And will return 2079 in case of 2 079 EUR

A working solution should return even: 207915 in the 1st case and 207900in the 2nd case or 2079.15 in the 1st case and 2079in the 2nd case.

The solution : https://stackoverflow.com/a/559178/1086682 works in case of local "US" or "en-GB" but does not work for "FR".

Proof

Hassan ALAMI
  • 318
  • 1
  • 5
  • 18
  • 1
    You should not use any floating point values for currency. Ever. Only integers, i.e. cents instead of dollars (similar for other currencies). Integer math is safe up to `Number.MAX_SAFE_INTEGER`, floating-point based math never is (famous 0.1+0.2). – Mörre Nov 21 '17 at 15:07
  • can you please clarify your answer ? – Hassan ALAMI Nov 21 '17 at 15:14
  • Don't use floating point numbers, use integers. Use cents, not dollars. – Mörre Nov 21 '17 at 15:15
  • why are you talking about dollars ? your comment is not helping me getting the desired result in any way – Hassan ALAMI Nov 21 '17 at 15:43
  • @HassanALAMI, don't edit the answer into the question. – Cerbrus Nov 21 '17 at 15:53
  • DO NOT USE FLOATING POINT FOR CURRENCY! USE INTEGERS! -- There - easier to understand now? Of course you could just read what I already wrote. It's very, very simple English. You keep asking "How do I hit myself on the head, very hard" and I keep telling you not to do that. – Mörre Nov 21 '17 at 15:53
  • i'm asking for float numbers in very simple English. – Hassan ALAMI Nov 21 '17 at 15:59
  • 1
    Möre, although you should never store a currency as a float you need to convert a string to a float, multiply it with 100 and then convert it to an integer. So you’ll need to convert the string to a float at some point. Unless you want to fuddle around with arrays that is... – patrick Nov 21 '17 at 19:02

0 Answers0