Re: uc_order_get_total() rounding problem

Posts: 2271
Joined: 08/07/2007
AdministratoreLiTe!

Due to the way floating point math works, it's almost impossible to get extremely accurate answers. In fact, the closer to 0 you get, the more noticeable it is.

There are two ways to deal with it, depending on what you want to do with the value:
1. Round
2. Allow for a margin of error in comparisons.

uc_currency_format() automatically rounds the input to two decimal places, so if you're displaying, this function should be used.

Never test float values for equality with 0. Instead, make sure it's smaller than a reasonably small number. For money, 0.001 is a good candidate.

If Ubercart needs to do one of these things, let us know where the value is being used.

uc_order_get_total() rounding problem By: z3b (2 replies) Thu, 05/08/2008 - 13:30