Link to home
Start Free TrialLog in
Avatar of ycomp
ycomp

asked on

How can I round a double to the nearest 0.5 or 0.25 ?

Hi, how can I round a double to the nearest 0.5 or 0.25 ?

e.g.

rounding to nearest 0.25:

4.08 -> 4
4.19 -> 4.25
5.55 -> 5.50
6.78 -> 6.75
6.98 -> 7

rounding to nearest 0.50:

4.08 -> 4
4.19 -> 4
5.55 -> 5.50
6.78 -> 7
6.98 -> 7
Avatar of zzynx
zzynx
Flag of Belgium image

 return Math.ceil( val * 0.25d ) / 0.25f ;
and
  return Math.ceil( val * 0.5d ) / 0.5f ;
Stupid me. That's wrong sorry.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
SOLUTION
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
... and of course 'd' is the original value
Avatar of ycomp
ycomp

ASKER

thanks CEHJ
:-)