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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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