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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare values of two .toFixed(2) variables 1

Status
Not open for further replies.

ppeterso

Programmer
Feb 3, 2005
3
US
Here's the problem... A user enters a total amount and then breaks down the total amount by budget account numbers. The total amount and summed amounts by budget numbers must match.

i.e. Stipend Total = 982.79

bgt #1 = 245.88
bgt #2 = 405.30
bgt #3 = 331.61

bgts 1,2,3 = 982.79 but program returns

Stipend total = 982.79
Bgt amts totals = 982.790000000000001

Here is how I have tried to fix this...
I have converted the Bgt amount totals and the original stipend amount to .toFixed(2) amounts and then compare the two amounts.

The program still returns "Amounts don't match"

Here is sample code:

if (whatever)
{
var n1 = parseFloat(theForm.StipendTotal.value);
alert("n1 = " + n1);
var n4 = n1.toFixed(2);
alert("n4 = " + n4); //returns 982.79
var n2 = parseFloat(theForm.BgtAmt1.value) + parseFloat(theForm.BgtAmt2.value) + parseFloat(theForm.BgtAmt3.value) + parseFloat(theForm.BgtAmt4.value) + parseFloat(theForm.BgtAmt5.value)
alert("n2 = " + n2);
var n3 = n2.toFixed(2);
alert ("n3 = " + n3); //returns 982.79

if (n3 != n4); //Here is the comparison of the two converted variables. Based on my alerts, both numbers match, but this error is still returned
{
alert("Stipend Total is: " + theForm.StipendTotal.value + "\rBudget Amounts Total: " + parseFloat(parseFloat(theForm.BgtAmt1.value) + parseFloat(theForm.BgtAmt2.value) + parseFloat(theForm.BgtAmt3.value) + parseFloat(theForm.BgtAmt4.value) + parseFloat(theForm.BgtAmt5.value)) + "\r\rThey must match.");
return (false);
}
}

My toFixed alerts both return 982.79. Does anyone know why these don't appear to be a match?

Any help would be appreciated.

Thank you,
Patrice

 
You are making this way too difficult for yourself.

All you need to do is this:
Code:
if (n3[!].toFixed(2)[/!] != n4[!].toFixed(2)[/!]);  //Here is the comparison of the two converted variables.  Based on my alerts, both numbers match, but this error is still returned


[monkey][snake] <.
 
Thank you. This worked. I was close so thank you again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top