Solved

# Function to convert between currency (formatted) and float (non-formatted)

Posted on 2007-07-31
1,511 Views
Hello,

I am looking for two functions.  One that can be passed a floating point number (ex: 1234.56) and returns the number formatted as currency (\$1,234.56) AND the reverse function (\$1,234.56 -> 1234.56)

I'm (nearly) certain that the solution would make use of regular expressions, so even just having the proper expressions would be adequate

Validation of the input is not necessary (I can do that separately) ...

Thanks!
0
Question by:Argblat

LVL 5

Accepted Solution

function formatCurrency(num) {
num = num.toString().replace(/\\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '\$' + num + '.' + cents);
}

to do the reverse, just replace the '\$' and the ',' with an empty string ("");

something like this.value.replace("\$","").replace(",","")
0

LVL 4

Assisted Solution

function formatCurrency(strValue)
{
strValue = strValue.toString().replace(/\\$|\,/g,'');
dblValue = parseFloat(strValue);

blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
dblValue = Math.floor(dblValue*100+0.50000000001);
intCents = dblValue%100;
strCents = intCents.toString();
dblValue = Math.floor(dblValue/100).toString();
if(intCents<10)
strCents = "0" + strCents;
for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
dblValue.substring(dblValue.length-(4*i+3));
return (((blnSign)?'':'-') + '\$' + dblValue + '.' + strCents);
}

function unformatCurrency(strValue)
{
strValue = strValue.replace("\$","").replace(",","");
return parseFloat(strValue);
}
0

## Join & Write a Comment Already a member? Login.

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be \$37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

#### 746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!