I would like to convert a string "$1,258.98" into a number 1258.98. How to use JavaScript to achieve this purpose?
Asked
Active
Viewed 69 times
1 Answers
1
Simply replace
$and,in the string with empty string and then useparseFloatfunction to convert it to a valid floating point number.var data = "$1,258.98"; console.log(parseFloat(data.replace("$", "").replace(",", "")));Or replace just the
,and then ignore the first character withsubstring(1)like thisconsole.log(parseFloat(data.replace(",", "").substring(1)));Or you can use regular expression, to replace
$and,, like thisconsole.log(parseFloat(data.replace(/[$,]/g, "")));
Output
1258.98
thefourtheye
- 233,700
- 52
- 457
- 497