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
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);

Link to comment
Share on other sites

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.