Rounding to nearest half

Malcy

Registered User.
Local time
Today, 11:04
Joined
Mar 25, 2003
Messages
586
Hi
I have a rounding problem that I cannot seem to hack through.
99% of the time the entry in a field (which has to be calculated) is either an integer or a precise half - i.e. 10 or 10.5 for instance.
Occasionally the computation goes to 10.0001 or 10.49998 where the environment has peculiarities.
I need to round either to an integer or to a half, but it isn't as simple as rounding up or rounding down.
If the value is between 10.750 and 11.250 I would want to round to 11 but if it was between 11.251 and 11.749 I would want to round to 11.5.
The issue is not fixed to these numbers since the whole number could be anything.

I am currently using
Code:
    Me.txtNumRound = (Int(Me.txtNum * 2)) / 2
and this rounds down to the half. If I change the 2 to -2 it rounds up to the half. If I change the 2 to 4 it does the same but to the quarter.

I think I need a sort of if test which says if the value after the decimal point is between x and y then round down to the nearest half or if between a and b then round up to the nearest half.
But I might be trying to create a sledge hammer to crack a nut.
Any suggestions would be most welcome - and yes I know you shouldn't store a calculated field but in this instance there are very good reasons why!
Thanks in anticipation
Best wishes
 
How about using Round?

?Round(10.0001 ,1)
10
?ROund(10.49998,1)
10.5
 
Thanks namliam
I had not come across this but think it will do since my calculation should get it close enough to round to the integer or the half
Thank you
It gets tricky when you have to delve into aspects you haven't covered before but there is usually a solution!
Thanks again
Best wishes
 

Users who are viewing this thread

Back
Top Bottom