Reputation: 19
<span id="lblSellingPrice" class="productHasRef">£11.00</span>
Hi i have this code.I like remove this £ and and then multiply the number by 2.50? Could this. Magnitude sleeping variable is not always 11.
Upvotes: 0
Views: 81
Reputation: 115222
You can use text()
and replace()
methods for that
$('#lblSellingPrice').text(function(i,v){
return '£'+(parseFloat(v.replace('£',''))*2.50).toFixed(2);
});
toFixed(2)
can be used to set only two decimal points
Upvotes: 1
Reputation: 10179
Try this:
$(document).ready(function () {
text = $("#lblSellingPrice").text().replace("£", ""); //remove the pound sign
num = parseInt(text); //parse the text into an integer
alert(num*2.5); //alert the multiplied value
});
Or more optimized:
parseInt($("#lblSellingPrice").text().replace("£", "")) * 2.5 //parse the text of the span by removing the pound sign and then multiply it by 2.5
Upvotes: 0
Reputation: 3937
Get the text of the span and parse it to float after removing the preceding '£' sign. Then you can multiply it by 2.5
var price = parseFloat($('#lblSellingPrice').text().substring(1));
var calculationResult = price * 2.5;
and if you want to put the currency mark back and format it to a price:
var newPrice = '£' + calculationResult.toFixed(2); //toFixed will give you 2 decimals
Upvotes: 3
Reputation: 818
This is a duplicate of this question (more or less):
Javascript extracting number from string
although if you don't have commas in your input number you don't need something as complex as the regex, you can just trim your currency marker off with a simple substring:
http://www.w3schools.com/jsref/jsref_substring.asp
Upvotes: 0
Reputation: 133403
Simply use replace
var num = $('#lblSellingPrice').text().replace('£', '');
num = parseFloat(num)*2.5;
Upvotes: 1
Reputation: 57095
var num = parseFloat($('#lblSellingPrice').text().substring(1))*2.5
Upvotes: 1