dlebowski Posted January 31, 2008 Share Posted January 31, 2008 $ItemTotalPrice=90000 $TaxTotal=900 $CashPremiumDue=4545.00 $CashTotalPaid=0 $GrandTotalNoPremium = ($ItemTotalPrice + $TaxTotal) + $CashPremiumDue; $AllPaymentTotal = $CashTotalPaid; $amountdue = number_format(($GrandTotalNoPremium - $AllPaymentTotal), 2, '.', ','); print "$cym"; print $amountdue; For some reason, $amountdue=90904 instead of 95445. I do not have this problem when $ItemTotalPrice is a smaller value like $1000. This works fine. Has anyone else seen anything like this before? Quote Link to comment https://forums.phpfreaks.com/topic/88693-solved-problems-with-larger-number-calulations/ Share on other sites More sharing options...
nethnet Posted January 31, 2008 Share Posted January 31, 2008 The problem is with the decimal in your variable $CashPremiumDue. When you perform mathematical operations in PHP with variables, by default, those variables are converted to integers if they are not already. Therefore, since $CashPremiumDue is type float instead of type integer, PHP is recognizing it and treating it the same way it would a string (because of the decimal). When you add or subtract strings in PHP, only the first character in the "string" is evaluated, so really PHP is recognizing this: <?php $GrandTotalNoPremium = (90000 + 900) + 4; ?> Obviously that's not what you want, so try something like this instead: <?php $GrandTotalNoPremium = ($ItemTotalPrice + $TaxTotal) + (int)$CashPremiumDue; ?> Or just drop the .00 if it's always zero. Quote Link to comment https://forums.phpfreaks.com/topic/88693-solved-problems-with-larger-number-calulations/#findComment-454196 Share on other sites More sharing options...
dlebowski Posted January 31, 2008 Author Share Posted January 31, 2008 Thanks for the explanation. This will always be a decimal though because is it a monetary value. How would I handle that? Quote Link to comment https://forums.phpfreaks.com/topic/88693-solved-problems-with-larger-number-calulations/#findComment-454451 Share on other sites More sharing options...
nethnet Posted January 31, 2008 Share Posted January 31, 2008 Hrmm, actually, I just ran your original code and it worked fine for me.. no problems. I don't know why it's adding up different for you. <?php ini_set(display_errors, TRUE); $ItemTotalPrice=90000; $TaxTotal=900; $CashPremiumDue=4545.00; $CashTotalPaid=0; $GrandTotalNoPremium = ($ItemTotalPrice + $TaxTotal) + $CashPremiumDue; $AllPaymentTotal = $CashTotalPaid; $amountdue = number_format(($GrandTotalNoPremium - $AllPaymentTotal), 2, '.', ','); echo $amountdue; ?> And this is what I see on the page: 95,445.00 Apparently the float vs. integer issue isn't even a problem here... I guess that just applies to strings. Try copying what is written above exactly and see if you still get 90,904.00 in your variable. Theo Quote Link to comment https://forums.phpfreaks.com/topic/88693-solved-problems-with-larger-number-calulations/#findComment-454480 Share on other sites More sharing options...
dlebowski Posted January 31, 2008 Author Share Posted January 31, 2008 $GrandTotalNoPremium = ($ItemTotalPrice + $absenteebid + $TaxTotal) + $CashPremiumDue; $AllPaymentTotal = (($CashTotalPaid + $CheckTotalPaid + $CreditTotalPaid) - $CashPremiumPaid); $amountdue = number_format(($GrandTotalNoPremium - $AllPaymentTotal), 2, '.', ','); } ?> </td> <? print $ItemTotalPrice; ?><br><? print $TaxTotal; ?> <br><? print $CashPremiumDue; ?> <br><? print $CashTotalPaid; ?> <br><? print $absenteebid; ?> <br><? print $CashTotalPaid; ?> <br><? print $CheckTotalPaid; ?> <br><? print $CreditTotalPaid; ?> <br><? print $CashPremiumPaid; ?> <br><? print $amountdue; ?> Here is what I get for the values: 90000 900 4,545.00 0.00 0 0.00 0.00 0.00 0 90,904.00 I did create a file called test.php and ran the code in it and it does calculate correctly. But if I run it within the original script, I get the results above. I don't understand how this is happening. Quote Link to comment https://forums.phpfreaks.com/topic/88693-solved-problems-with-larger-number-calulations/#findComment-454506 Share on other sites More sharing options...
dlebowski Posted January 31, 2008 Author Share Posted January 31, 2008 I think I may have figured it out. It doesn't like me to use number_format for both my $CashPremiumDue variable and my $amountdue value. I was using number_format to generate the $CashPremiumDue value. change from: $CashPremiumDue=number_format(($GrandTotalNoPremium * $BuyerCashPrem), 2, '.', ','); to: $CashPremiumDue=round (($GrandTotalNoPremium * $BuyerCashPrem), 2); This appears to have worked although I am off by .01 in a lot of the cases. I will have to maybe play with the rounding to take care of this. Why would it not let me use number_format for both $amountdue and $CashPremiumDue? Quote Link to comment https://forums.phpfreaks.com/topic/88693-solved-problems-with-larger-number-calulations/#findComment-454600 Share on other sites More sharing options...
PFMaBiSmAd Posted January 31, 2008 Share Posted January 31, 2008 The .01 is probably due to the inexact representation of floating point numbers in binary and binary conversion to floating point and back for display (BTW: using a floating point number in a calculation causes that calculation to be done in floating point.) Take a look at the BC (BCD) math functions - http://www.php.net/manual/en/ref.bc.php They essentially work like a calculator would, so the conversion errors to/from binary for floating point numbers are eliminated. Quote Link to comment https://forums.phpfreaks.com/topic/88693-solved-problems-with-larger-number-calulations/#findComment-454676 Share on other sites More sharing options...
dlebowski Posted January 31, 2008 Author Share Posted January 31, 2008 I can't tell you how much I appreciate all the help. bcmul works perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/88693-solved-problems-with-larger-number-calulations/#findComment-454695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.