views:

4946

answers:

4

I have a text box that will have a currency string in it that I then need to convert that string to a double to perform some operations on it.

"$1,100.00" -> 1100.00

This needs to occur all client side. I have no choice but to leave the currency string as a currency string as input but need to cast/convert it to a double to allow some mathematical operations.

A: 

I know you've found a solution to your question, I just wanted to recommend that maybe you look at the following more extensive jQuery plugin for International Number Formats:

International Number Formatter

NTulip
+1  A: 

You can try this

<script type="text/javascript">

var str="$1,112.12";
str = str.replace(",","");
str = str.replace("$","");
document.write(parseFloat(str));

</script>
Ólafur Waage
+2  A: 

Use a regex to remove the formating (dollar and comma), and use parseFloat to convert the string to a floating point number.`

var currency = "$1,100.00";
currency.replace(/[$,]+/g,"");
var result = parseFloat(currency) + .05;
Jamie
+4  A: 

Remove all non dot / digits:

var currency = "$1,100.00";
var number = Number(currency.replace(/[^0-9\.]+/g,""));
CMS
seems to convert perfectly for me, thanks
Bobby Borszich
+1 (Sorry for the previous comment, the fact that you were actually *excluding* the dot as well didn't occur to me for some reason.)
Tomalak