I have a number that I need to pull from my html:
<span>123,456.78</span>
How can I convert this string into a number that I can do math on?
var numberString = $('span').text();
var realNumber = Number(numberString); //returns NaN
A jQuery-only solution would be okay.
I have a number that I need to pull from my html:
<span>123,456.78</span>
How can I convert this string into a number that I can do math on?
var numberString = $('span').text();
var realNumber = Number(numberString); //returns NaN
A jQuery-only solution would be okay.
Share Improve this question edited Sep 14, 2011 at 15:05 Andrew asked Sep 13, 2011 at 22:37 AndrewAndrew 239k195 gold badges531 silver badges718 bronze badges3 Answers
Reset to default 7parseInt() or parseFloat() would just about do it.
var number = parseFloat($('span').text());
after checking and seeing this doesn't work...
try
var number = $('span').text().replace(/([^0-9\.])/g,"");
var number = parseFloat($('span').text().replace(/([^0-9\\.])/g,""));
I'm not sure what realNumber
does, but here's how I'd convert that string into a number:
var numberString = $('span').text();
var amount = + numberString.replace(/,/g, '');
This removes the mas, then uses the +
unary operator to convert the string to a number. In your example, the result is the number 123456.78
.
updated
var numberString = $('span').text();
var number = Number(numberString.replace(/[^0-9\.]+/g,""));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743659133a4485891.html
评论列表(0条)