What can I use to convert this string into a number? "$148,326.00"
I am guessing that I need to explode it and take the dollar sign off, and then use parseFloat()? Would that be the wisest way?
This is how I am getting the number:
var homestead = xmlDoc.getElementsByTagName("sc2cash");
document.getElementById('num1').innerHTML = homestead[1].textContent;
You need to remove the dollar signs and commas, (string replace), then convert to a float
value
Try this:
parseFloat('$148,326.00'.replace(/\$|,/g, ''))
See: http://www.w3schools.com/jsref/jsref_parseFloat.asp
Or: http://www.bradino.com/javascript/string-replace/
To handle other currency symbols you could use the following instead (which will remove all non numeric values (excluding a .
and -
)):
parseFloat('$148,326.00'.replace(/[^0-9.-]+/g, ''))
var s = '$148,326.01';
parseFloat(s.replace(/[^\d.]/g, '')); // => 148326.01
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With