user3234510
user3234510

Reputation: 19

How to remove £ on span and change value?

<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

Answers (6)

Pranav C Balan
Pranav C Balan

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

FIDDLE

Upvotes: 1

Mohammad Areeb Siddiqui
Mohammad Areeb Siddiqui

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

sjkm
sjkm

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

DrLivingston
DrLivingston

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

Satpal
Satpal

Reputation: 133403

Simply use replace

 var num = $('#lblSellingPrice').text().replace('£', '');
 num = parseFloat(num)*2.5;

Demo

Upvotes: 1

Demo Fiddle

var num = parseFloat($('#lblSellingPrice').text().substring(1))*2.5

Upvotes: 1

Related Questions