i have the following tag in html i do not want the $ to be taken as i just want the price for calculations purpose.
<span id="testspan">$101.82</span>
in the above span tag i want to take only 101.82 value for calculations .
I am using the html() to get the value
var csqft_price = $('#testspan').html();
var price= parseFloat(csqft_price);
but i am getting it with $ so i am unable to do calculation how can i do it.
You can use
var csqft_price = $('#testspan').html();
var number = Number(csqft_price.replace(/[^0-9\.]+/g, ""));
This was already posted on SO here.
The following tests whether the first character is a dollar-sign and if so takes the rest of the string:
var csqft_price = $('#testspan').html();
var price = +(csqft_price.charAt(0)==="$" ? csqft_price.substr(1) : csqft_price);
Of course, if you know that there will always be a dollar-sign you can just do this:
var price = +csqft_price.substr(1);
Note: I generally prefer the unary plus operator to convert a string to a number, so I've used that in my answer above - you can change +(...)
to parseFloat(...)
if you prefer.
I would replace all non digets and dots with nothing and make than a parseFloat:
var textValue = '$101.82';
var floatedValue = parseFloat(textValue.replace(/[^\d\.]/, ''));
alert(floatedValue);
Example: http://jsfiddle.net/MEY9R/
Use this
var csqft_price = $('#testspan').html();
var price=parseFloat(csqft_price.replace('$',''));
alert(price);
And use price for calculation.
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