Jump to content

Recommended Posts

$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?

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.

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

$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. 

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.