Imad Posted January 4, 2010 Share Posted January 4, 2010 Hey Guys, I'm starting to get a little frustrated here with this. I'm performing a simple subtraction here $account_balance - $price. Let's say the account balance was 100.01, and the price was 5.00. The end result when I perform the subtraction is: 95.00 when it should be 95.01. Is there anyway to get php to stop rounding? I gave round & double a try to no avail. Thanks Much. Quote Link to comment https://forums.phpfreaks.com/topic/187137-stop-rounding-number/ Share on other sites More sharing options...
premiso Posted January 4, 2010 Share Posted January 4, 2010 Post the code you are using as it is hard to see what is going wrong without the actual code. Quote Link to comment https://forums.phpfreaks.com/topic/187137-stop-rounding-number/#findComment-988239 Share on other sites More sharing options...
roopurt18 Posted January 4, 2010 Share Posted January 4, 2010 Floating point numbers are approximations to their "real" values. 95.01 could be one of those numbers that actually has no representation. I recommend using a math library that supports arbitrary precision if this is a concern for you. Quote Link to comment https://forums.phpfreaks.com/topic/187137-stop-rounding-number/#findComment-988299 Share on other sites More sharing options...
ignace Posted January 4, 2010 Share Posted January 4, 2010 I'll second roopurt, use http://php.net/manual/en/book.bc.php Quote Link to comment https://forums.phpfreaks.com/topic/187137-stop-rounding-number/#findComment-988303 Share on other sites More sharing options...
Imad Posted January 4, 2010 Author Share Posted January 4, 2010 Thanks for the help guys. Since my system is working with dollar amounts, the penny makes a difference. Additionally, no item will cost half a penny, or break the two decimal barrier. However, to make sure it doesn't, I decided to create my system to remove the cents if it's not going to affect the deduction, then append them after the arithmetic calculations. If the price affects the account balance's cents, then it performs the calculations as normal. This seems like it's working excellently and performed all sorts of testing. The entire system would have been easier if the user didn't have 2 different balances, one is the deposited amounts, and the other is the earnings they make on the site. Since purchasing allows them to use both balances it makes everything more difficult. I will continue testing my system with different senarios and see if it's the ideal way of doing things, otherwise, I'll go with the arbitrary precision library. Thanks again for the help. Quote Link to comment https://forums.phpfreaks.com/topic/187137-stop-rounding-number/#findComment-988348 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.