I'm bothering you because I'm looking to convert a string into a float with node.JS.
I have a string that comes back from a request and looks like:
x,xxx.xxxx
(ex. 4,530.1200
) and I'd like to convert it to a float: xxxx.xxxx
(or 4530.1200
).
I've tried to use:
parseFloat(str)
but it returns 4
. I also found Float.valueOf(str)
on the web but I get a ReferenceError: Float is not defined
error.
You can use string replace to solve this kind of problem.
str = '4,530.1200';
parseFloat(str.replace(/,/g, ''));
This will remove all the ,
from your string so that you can convert it to the float using parseFloat
.
Notice the g
flag in regex, it represents global
, by using this you are specifying to remove all the matched regex.