Link to home
Start Free TrialLog in
Avatar of frimy
frimyFlag for United States of America

asked on

how to round to the nearest .25 (quarter)

Hello ALL!
how to round to the nearest quarter .25
i.e.
1.20 =  1.25
1.30 =  1.50
1.65 =  1.75
1.80 =  2.00
thanks in advance

Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rounding to the nearest quarter would return these results:

 1.2           1.25 
 1.3           1.25 
 1.65          1.75 
 1.8           1.75 

Open in new window

which can be reached using Format:

RoundedAmount = CCur(Format(Amount * 4, "0")) / 4

Open in new window

However, your example values show values rounded up. That can be done using Daniel's expression.

For all sorts of rounding, see VBA.Round.
Avatar of frimy

ASKER

Daniel,
your solution works beautiful,
but one problem, it adds a minus sign before the number.

a = 33.60
b = Int(-a * 4) / 4
b = -33.75

what can we do?
thanks

what can we [you] do?
Use Daniel's code as originally posted ...
Avatar of frimy

ASKER

i can do to add Abs()
i did use the same code as posted
i just want to know what Daniel can do.

I did use the same code as posted
Well, look again, this time carefully.

You cannot apply Abs, as that will prohibit the rounding of negative values.
Avatar of Daniel Pineault
Daniel Pineault

My original code included a - at the beginning that you missed which addresses this issue.
Be aware of the domain used. If you multiply a value by 4  you should not overflow the values.
Also be aware of rounding behaviour...    multiply by 4  device by 4 is a binary neutral operation (shift left2, shitft right 2 bits).

With non 2^n numbers you introduce more rounding due to "non-fitting" data (there is no 1/3rd bit).