Replace ,
from the string before passing the string to the parseFloat
If the string contains more than one ,
, you can use ragex to remove all occurrences of the ,
.
The regex /,/
will match the ,
literal from the string and g
the global flag will make the regex to match all possible strings i.e. all the ,
from string.
var str = "1,722,311.75";
var nr = parseFloat(str.replace(/,/g, ''), 10).toFixed(2);
document.write(nr);
If the string contains only one ,
, you can use replace
with string to remove ,
from the string.
var str = "722,311.75";
var nr = parseFloat(str.replace(',', ''), 10).toFixed(2);
document.write(nr);
Note: replace(',', '')
will remove only first occurrence of ,
. See first code snippet for regex approach that will replace all the occurrences.