views:

11032

answers:

2

I have a datagrid and I get 2 column's value by javascript. These fields are numeric and when fields have comma (ex. 554,20), I cant get the numbers after comma. I tried parseInt, parseFloat. How can I solve it?

+4  A: 

Replace the comma with a dot.

This will only return 554:

var value = parseFloat("554,20")

This will return 554.20:

var value = parseFloat("554.20")

So in the end, you can simply use:

var fValue = parseFloat(document.getElementById("textfield").replace(",","."))

Don't forget that parseInt() should only be used to parse integers (no floating points). In your case it will only return 554. Additionally, calling parseInt() on a float will not round the number: it will take its floor (closest lower integer).

Wadih M.
+11  A: 

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(",", "."));
Jesse Rusak
What about where you have a comma used as a thousand seperator? E.g. 1234 written as 1,234
You could just remove the comma, then parse it. (eg. replace it with "")
Jesse Rusak