Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency Conversion Function

Status
Not open for further replies.

budapestnori

Technical User
Jan 15, 2004
33
HU
This function is working great, until a number 2000 or above is introduced, why? I didn't write it, i got it from the net.

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);
}
 
OOOPs, maybe I am mistaken. Here are more details. I am using it on this page -
When you go above a total of 2,000 in the context of my code, is when the format currency is not working, so it must be a bug in my code,and not the above code.

Here is my code:

function calculatePrintTotal(objx,price,img_total)
{
x = parseFloat(objx.value);
y = parseFloat(price);
z = parseFloat(img_total.value);

t = formatCurrency((z+(x*y)));
img_total.value = formatCurrency(t);

}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top