8

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.

Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
user818671
  • 389
  • 4
  • 8
  • 21

7 Answers7

19

You can use

var csqft_price = $('#testspan').html();
var number = Number(csqft_price.replace(/[^0-9\.]+/g, ""));

This was already posted on SO here.

jsouthwo
  • 15
  • 4
Hemant Metalia
  • 29,730
  • 18
  • 72
  • 91
3

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.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
  • The sample number in the question didn't go into the thousands, but if you might have a number with the format `$12,000.01` (i.e., with commas) then I'd recommend using a `.replace()` like in Hemant's answer (it removes all characters except digits and full-stops). – nnnnnn Dec 28 '11 at 10:23
  • Casting to a number using `parseFloat` is more permissive than `+`. See also: [Are there are any side effects of using this method to convert a string to an integer](http://stackoverflow.com/questions/8112757/are-there-are-any-side-effects-of-using-this-method-to-convert-a-string-to-an-in/8112802#8112802) – Rob W Dec 28 '11 at 10:26
  • @Rob W - Sure, but the only difference is it ignores non-numeric trailing characters isn't it? I would use `parseFloat()` only when I explicitly wanted that effect. – nnnnnn Dec 28 '11 at 11:28
  • @nnnnnn In this case, it shouldn't matter, since the format is predefined. I just mentioned the difference as a FYI ;) – Rob W Dec 28 '11 at 11:30
2

Use this

var csqft_price = $('#testspan').html();
var price=parseFloat(csqft_price.replace('$',''));
alert(price);

And use price for calculation.

Kelvin
  • 1,004
  • 1
  • 10
  • 32
2

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/

Armin
  • 15,582
  • 10
  • 47
  • 64
  • 1
    Please note, this fails if there is a comma in the number as is common in Canada/US (among other countries) EG: 1,101.82, http://jsfiddle.net/nathankoop/MEY9R/1/ – Nathan Koop Nov 01 '12 at 19:44
0

ku4js-kernel has a sweet $.money class for this. You can just $.money.parse("$101.82") and then do what you want.

You could get the value() and use as a number or even better, just keep as money and do your operations. You can check out the documentation here.

0

Try replacing the $ sign by using the javascript "replace" function and replace the $ sign with an empty value, like this:

var vsqft_price = $('#testspan').html();
var vsqft_float = vsqft_price.replace("$", "");
var price= parseFloat(csqft_float);
Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • Not flexible enough. Just work for dollars and does not respect any other possible chars (like spaces) – Armin Dec 28 '11 at 10:19
  • @Armin - this answers the question as asked, where the format seems to be known (and doesn't have spaces). – nnnnnn Dec 28 '11 at 10:21
  • Yes, but the other solution (via regexp) fits your requirements too and is a much more flexible way to solve it! Other users who read this, should know about this fact. – Armin Dec 28 '11 at 10:25
0

Try this -

   var csqft_price = $('#testspan').text();
    var s = csqft_price.substr(1);
    alert(s);

http://www.w3schools.com/jsref/jsref_substr.asp

Vikas Naranje
  • 2,350
  • 5
  • 30
  • 40