Nandini Posted May 30, 2011 Share Posted May 30, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/237849-adding-positive-and-negative-numbers/ Share on other sites More sharing options...
silkfire Posted May 30, 2011 Share Posted May 30, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/237849-adding-positive-and-negative-numbers/#findComment-1222244 Share on other sites More sharing options...
Nandini Posted May 30, 2011 Author Share Posted May 30, 2011 Thanq very much Its working fine Quote Link to comment https://forums.phpfreaks.com/topic/237849-adding-positive-and-negative-numbers/#findComment-1222247 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.