From: Roedy Green on
If you use double to compute sales taxes, and round to the nearest
penny, there is a gotcha. You are supposed to round a precise half
penny up. This won't work properly for percentage like 6% (0.06) (of
$0.75) which is not precise in binary.

There a number of ways to avoid problem. Which would you use?
--
Roedy Green Canadian Mind Products
http://mindprod.com

What is the point of a surveillance camera with insufficient resolution to identify culprits?
From: Arne Vajhøj on
On 08-05-2010 17:42, Roedy Green wrote:
> If you use double to compute sales taxes, and round to the nearest
> penny, there is a gotcha. You are supposed to round a precise half
> penny up. This won't work properly for percentage like 6% (0.06) (of
> $0.75) which is not precise in binary.
>
> There a number of ways to avoid problem. Which would you use?

BigDecimal should be able to mimic the rules out of the box.

Integers (long or int) could be made to mimic the rules.

And as a general rule there is death penalty for using
floating point for amounts.

Arne

From: Lew on
On 05/08/2010 05:42 PM, Roedy Green wrote:
> If you use double to compute sales taxes, and round to the nearest
> penny, there is a gotcha. You are supposed to round a precise half
> penny up. This won't work properly for percentage like 6% (0.06) (of
> $0.75) which is not precise in binary.

In what jurisdictions? That's not the rule in the parts of the U.S. with
which I'm familiar. Sales tax rounds up, not to the nearest.

> There a number of ways to avoid problem. Which would you use?

I'd conform to the law.

--
Lew
From: Arne Vajhøj on
On 08-05-2010 19:43, Lew wrote:
> On 05/08/2010 05:42 PM, Roedy Green wrote:
>> If you use double to compute sales taxes, and round to the nearest
>> penny, there is a gotcha. You are supposed to round a precise half
>> penny up. This won't work properly for percentage like 6% (0.06) (of
>> $0.75) which is not precise in binary.
>
> In what jurisdictions? That's not the rule in the parts of the U.S. with
> which I'm familiar. Sales tax rounds up, not to the nearest.

Give that Roedy is Canadian then I would assume Canada.

>> There a number of ways to avoid problem. Which would you use?
>
> I'd conform to the law.

And use something else than double.

Arne
From: Arved Sandstrom on
Lew wrote:
> On 05/08/2010 05:42 PM, Roedy Green wrote:
>> If you use double to compute sales taxes, and round to the nearest
>> penny, there is a gotcha. You are supposed to round a precise half
>> penny up. This won't work properly for percentage like 6% (0.06) (of
>> $0.75) which is not precise in binary.
>
> In what jurisdictions? That's not the rule in the parts of the U.S.
> with which I'm familiar. Sales tax rounds up, not to the nearest.
>
>> There a number of ways to avoid problem. Which would you use?
>
> I'd conform to the law.

I'd use software that someone else wrote who had lots more time to
devote to the problem of sales tax arithmetic legalities than I do.

AHS