Jump to content

Adding positive and Negative numbers


Nandini

Recommended Posts

Hi all

 

I am adding some positive and negative numbers.

 

But i am getting strange output. Can anyone help me

 

Here is the code.

 

$sum=7.50+6.45+12.90+12.00+13.00+0.61+32.00+4.00+27.00+18.00+10.88+129.50+92.94-100.00-38.25+76.80-305.33;
echo "<br>Total: ".$sum;

 

Basically i have to get Total is 0

 

but i am getting total is 5.6843418860808E-14

 

Can anyone help me out Please

Link to comment
https://forums.phpfreaks.com/topic/237849-adding-positive-and-negative-numbers/
Share on other sites

It's because of the PHP Math precision. It happens when the result is 0;

 

305.33(75340675450) - 305.33(7534067545065231111)

 

There are hidden decimals in your number which PHP sees and uses them in the calculation.

 

Round your answer to 2 decimals:

 

$sum = round(7.50 + 6.45 + 12.90 + 12.00 + 13.00 + 0.61 + 32.00 + 4.00 + 27.00 + 18.00 + 10.88 + 129.50 + 92.94 - 100.00 - 38.25 + 76.80 - 305.33, 2);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.