I am trying to parse two values from a datagrid.
The fields are numeric, and when they have a comma (ex. 554,20), I can't get the numbers after the comma.
I've tried parseInt
and parseFloat
. How can I do this?
|
|||||
|
If they're meant to be separate values, try this:
If they're meant to be a single value (like in French, where one-half is written 0,5)
|
|||||||||||||||||||||
|
Have you ever tried to do this? :p
+(string) will cast string into float. Handy! So in order to solve your problem, you can do something like this:
|
|||||
|
Replace the comma with a dot. This will only return 554:
This will return 554.20:
So in the end, you can simply use:
Don't forget that |
||||
|
If you extend String object like this..
.. you can run it like this
works with dot as well
|
|||||
|
@GusDeCool or anyone else trying to replace more than one thousands separators, one way to do it is a regex global replace:
|
|||
|
I had the same problem except I did not know in advance what were the thousands separators and the decimal separator. I ended up writing a library to do this. If you are interested it here it is : https://github.com/GuillaumeLeclerc/number-parsing |
|||
|
protected by Community♦ Mar 21 '13 at 10:52
Thank you for your interest in this question.
Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?