[AccessD] Rounding percent midways

Gustav Brock gustav at cactus.dk
Tue Nov 13 09:58:33 CST 2012


Hi all

I had a bright moment(!) regarding rounding percent values, a topic which
from time to time has caught my attention.

The philosophy behind this is, that 0% is zero, not something very small,
and 100% is everything, not nearly everything. A progress bar showing 0%
means "nothing" and 100% means "all". 

Consequently, normal 4/5 rounding cannot be used as very small values would
be rounded to zero, and values nearly 100% would be rounded to 100%.
Rounding up would work for very small values but large values like 99.996%
would be rounded up to 100.00%. You could round down, but that would move
the issue to rounding very small values, say, 0.004% down to 0.00%. Thus,
neither rounding up or down can be used.

The solution is to round up for small values and down for large values or,
in other words, to round "towards" 50%. But how?
This is where Fix comes in. It rounds towards zero, so by off-setting the
value range of 0 to 1 to -0.5 to 0.5, Fix can be applied. After the
rounding, reset the range to cover 0 to 1.

Here is how:

dblValue = Fix((dblValue - 0.5) * 10000) / 10000 + CDec(0.5)

This will round, say, 0.004% and 99.996% to 0.01% and 99.99% respectively.
CDec is used to prevent floating point errors.

/gustav



More information about the AccessD mailing list