Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery pull currency without dollar sign

I have a tag that has a price in it (ie. $150.00). I want to use jQuery to pull only the text values without the dollar sign ($).

<div>
  $150.00
</div>

I want to output the above as 150.00

like image 448
ToddN Avatar asked Sep 27 '11 17:09

ToddN


1 Answers

You can use replace to replace the $ character with an empty string:

var price = $("div").text().replace("$", "");

Note that in the case of your exact example (with that spacing) the blank spaces will not be removed. If you're going on to use the string as a number (via parseFloat for example) that won't matter, but if you're wanting to use it as text somewhere else, you may want to remove white space with jQuery's .trim.

Update - based on comments

replace returns a String. Once you have that string, you can parse it into a Number with parseFloat (or parseInt, but you're working with floating point numbers):

var convertedToNumber = parseFloat(price);

Now that you've got a number, you can perform mathematical operations on it:

var percentage = convertedToNumber * 0.95;

like image 174
James Allardice Avatar answered Oct 13 '22 12:10

James Allardice