Gustav Brock
Gustav at cactus.dk
Tue Nov 13 15:52:00 CST 2012
Hi Stuart
Good idea. You could even make that Digits optional.
/gustav
>>> stuart at lexacorp.com.pg 13-11-12 21:06 >>>
That's a neat idea as long as you are aware of its effects and use it sensibly - i.e. not as a
general rounding function :-)
It's going into my toolbox( with one minor enhancement)
Function PCRound(Value As Double, Digits As Long) As Double
PCRound = Fix((Value - 0.5) * 10 ^ Digits) / 10 ^ Digits + CDec(0.5)
End Function
--
Stuart
On 13 Nov 2012 at 16:58, Gustav Brock wrote:
> 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