[Slightly OT] Dealing with fractions of cents

  • Thread starter Thread starter Chris Dunaway
  • Start date Start date
C

Chris Dunaway

I am writing a simple point of sale type application. One of the
functions includes reading a weight from a scale and then multiplying
that weight by a UnitPrice to get an amount.

The scale returns its weight in pounds to 3 decimal places. We then
take this weight and multiply by the unit price per pound which is
$3.20.

Sometimes, I get total amounts that have more than 2 decimal places.
For example, if the scale reads .995 lbs and I multiply by the unit
price os $3.20, the result is $3.184.

When a transaction is entered into the transactions table, using a
money column, the value is stored unrounded (i. e. as 3.184).

I use the Decimal data type in C# so this works out fine.... sort of.

Inevitably on printed reports, sometimes the sums of the values are off
by a penny or two. This seems to be due to a rounding of the actual
values to 2 decimal places.

I am looking for a way to handle smoothly so that calcuations and
reports agree.

Does anyone have any algorithms or advice on a simple way to handle
this type of situation?

Thanks,

Chris
 
What I would suggest is that as soon as you assign a price to anything,
round it to the nearest cent and store the rounded value.
 
Absolutely. I've worked on supermarket pricing systems, among other
areas. I've never seen a situation in which a price is held in
fractions of cents and then totaled later. If the math for the weighed
item works out to 3.184, the consumer pays 3.18, so that's what you
print on the bill.

Now, if you're storing information about how many pounds of whatever
were sold, which figures into cost, and the costs come out to three
decimal places (dollars) or one decimal place (cents) then the solution
is easy: just print three decimal places on the cost report.

However, the price at the till is 3.18. On the retail side it makes no
sense to keep the third decimal place per item.

The only area where I've seen this as a concern to consumers was in
cellular phone billing. The costs of individual calls worked out to
three decimal places when using per-second billing, but the consumer is
charged based on total time used. If you added up the itemized charges
on the bill they often disagreed with the total charged. We received
several phone calls a month about that.

In a weighed item scenario, though, I see no reason to hold the third
decimal place.
 
Back
Top