PDA

View Full Version : calculating to correct decimal for currency

jeanne
08-08-2002, 09:07 PM
i am creating a scrip to calculate price, totals extra for an order form.

right now the script round to the nearst 100 decimal place.

my problem is that if the total is 56.80
it appears as 56.8

how can i make sure it tacks on that zero when it is needed?

this is my bit of code..
___________________________________________________
// check to make sure quantity and price are both NOT not a number...
if (!isNaN(qnumber) && !isNaN(pnumber))
{
// multiply quanity and price
tnumber = (qnumber * pnumber);
tnumber = Math.round(tnumber * 100) / 100;
tcommand = "document.form.aidtotal"+row+".value = tnumber";
eval(tcommand);
aidsubtotal += tnumber
}
// and make sure they aren't blank
else if ((qvalue != "") && (pvalue != ""))
{
}
else
{
tcommand = "document.form.aidtotal"+row+".value = 0";
eval(tcommand);
}

__________________________________________________

does any one have any suggestions?
i feel like the answer is probably simple enough. i just cant seem to wrap my brain around it...

lhenry
lhenry@quinstreet.com

neil.c
08-08-2002, 09:35 PM
...
tnumber = Math.round(tnumber * 100) / 100;
if (10 * tnumber == Math.round(10 * tnumber))
tnumber += "0";
...

should do it.

BrainJar
08-08-2002, 11:21 PM
Or try

tnumber = tnumber.toFixed(2);

jeanne
08-09-2002, 01:51 AM
both of them worked. but not as completely as i need it to.

someone else sent me this little function whish works perfectly for my purposes.

function displayDollars(number) {
dollars = Math.floor(number);
cents = Math.round(number*100) - dollars*100;
if (dollars == 0) {
if (cents < 10) {
return "0.0" + cents;
} else {
return "0." + cents;
}
} else {
if (cents < 10) {
return dollars + ".0" + cents;
} else {
return dollars + "." + cents;
}
}
}

so then i just ran tnumber through this function. and taadaaa!

just thought i would post that solution incase anyone else could use it!