If they're meant to be separate values, try this:
var values = "554,20".split(",")
var v1 = parseFloat(values[0])
var v2 = parseFloat(values[1])
If they're meant to be a single value (like in French, where one-half is written 0,5)
var value = parseFloat("554,20".replace(",", "."));
Answer from Jesse Rusak on Stack OverflowIf they're meant to be separate values, try this:
var values = "554,20".split(",")
var v1 = parseFloat(values[0])
var v2 = parseFloat(values[1])
If they're meant to be a single value (like in French, where one-half is written 0,5)
var value = parseFloat("554,20".replace(",", "."));
Have you ever tried to do this? :p
var str = '3.8';ie
alert( +(str) + 0.2 );
+(string) will cast string into float.
Handy!
So in order to solve your problem, you can do something like this:
var floatValue = +(str.replace(/,/,'.'));
Videos
How to convert string to double in JS when it has spaces?
How to convert string to double in JavaScript when I read from input box?
How do I convert a string to double in JS if my string is empty?
Remove all non dot / digits:
var currency = "-$4,400.50";
var number = Number(currency.replace(/[^0-9.-]+/g,""));
accounting.js is the way to go. I used it at a project and had very good experience using it.
accounting.formatMoney(4999.99, "โฌ", 2, ".", ","); // โฌ4.999,99
accounting.unformat("โฌ 1.000.000,00", ","); // 1000000
You can find it at GitHub
Let string arithmetic do the string conversion, and then use parseFloat to get it back to a float:
var dA = 323423.23423423e4;
var sA = dA + '';
var dnA = parseFloat(sA);
Here's a fiddle to see it in action.
Note: All JavaScript numbers are doubles, so you don't need to draw a distinction between doubles and floats. Thus, the only place you'd need to worry about precision loss is in your first line. For example, if you did var dA = 987654321.0123456789 for your first line, it would be equivalent to var dA = 987654321.01234567, and dA would still equal dnA in the end.
Just dA.toString() and parseFloat(sA) should do it.