Jump to content

Stop rounding number?


Imad

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/187137-stop-rounding-number/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/187137-stop-rounding-number/#findComment-988299
Share on other sites

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.  :)

Link to comment
https://forums.phpfreaks.com/topic/187137-stop-rounding-number/#findComment-988348
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.