JudgementDay Posted February 6, 2012 Share Posted February 6, 2012 When I use this: echo number_format(150 + 20.95 + .5, 2); ... I get 170.95. Yet on my calculator, 150 + 20.95 + .5 gives me 171.45. Why is that? Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/ Share on other sites More sharing options...
kicken Posted February 6, 2012 Share Posted February 6, 2012 Works just fine for me. kicken@linode:~$ php -r 'echo number_format(150 + 20.95 + .5, 2);' 171.45 Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/#findComment-1315272 Share on other sites More sharing options...
DavidAM Posted February 6, 2012 Share Posted February 6, 2012 p0-mad:~$ php -r "echo number_format(150 + 20.95 + .5, 2);" 171.45 works for me, too. Are you using variables in your code? Maybe they are not what you think they are. Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/#findComment-1315273 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 works for me, too. Are you using variables in your code? Maybe they are not what you think they are. You were spot on! I was using variables and one was not what I thought. Thanks for reminding me. I had only been out of bed for 10 minutes, so the brain wasn't functioning properly. lol Well, now I actually do have a problem: echo number_format(150 + 20.95 * 1.01 + .5, 2); ... gives me 173.16 My calculator gives me: 171.65 Have I done something wrong with PHP? Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/#findComment-1315277 Share on other sites More sharing options...
kicken Posted February 7, 2012 Share Posted February 7, 2012 Again, works fine for me. Same variables problem? kicken@linode:~$ php -r 'echo number_format(150 + 20.95 * 1.01 + .5, 2);' 171.66 Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/#findComment-1315282 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 Well, I just ate breakfast and things are allot clearer. I failed at translating my PHP code to general maths for you guys. I should have actually said: echo number_format((150 + 20.95) * 1.01 + .5, 2); I removed the inner brackets and now things work fine. I cannot understand why that made a different? BTW, I saw you have a PHP CLI thing. So cool! I will get this today! Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/#findComment-1315294 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 Gezus! Now I have another problem. I swear when ever maths is involved everything goes wrong for me. PHP: 150 + 20.95 * 1.01 + .5 = 171.66 Calculator: 150 + 20.95 * 1.01 + .5 = 171.65 Whats up with that? Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/#findComment-1315296 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 Don't bother replying. Stupid own fault again. I am switching off this computer and going for a walk. I need to reboot. Big mistake jumping on the computer so early. Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/#findComment-1315297 Share on other sites More sharing options...
JudgementDay Posted February 7, 2012 Author Share Posted February 7, 2012 Just came back to say thanks guys. I sorted out my math mess. Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/#findComment-1315301 Share on other sites More sharing options...
litebearer Posted February 7, 2012 Share Posted February 7, 2012 http://php.net/manual/en/language.operators.precedence.php Quote Link to comment https://forums.phpfreaks.com/topic/256571-calculation-inconsistency/#findComment-1315329 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.