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 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); 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 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
Archived
This topic is now archived and is closed to further replies.