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
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
Stupid me. That's wrong sorry.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
... and of course 'd' is the original value
ASKER
thanks CEHJ
:-)
and
return Math.ceil( val * 0.5d ) / 0.5f ;