Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert money format into numbers [duplicate]

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.

like image 558
user818671 Avatar asked Dec 28 '11 10:12

user818671


4 Answers

You can use

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

This was already posted on SO here.

like image 60
Hemant Metalia Avatar answered Oct 05 '22 05:10

Hemant Metalia


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.

like image 25
nnnnnn Avatar answered Oct 05 '22 07:10

nnnnnn


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/

like image 45
Armin Avatar answered Oct 05 '22 07:10

Armin


Use this

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

And use price for calculation.

like image 45
Rajesh Kumar Maurya Avatar answered Oct 05 '22 06:10

Rajesh Kumar Maurya