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
ycompAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CEHJConnect With a Mentor Commented:
double x = (double)(Math.round(d * 4.0)) / 4;
0
 
zzynxSoftware engineerCommented:
 return Math.ceil( val * 0.25d ) / 0.25f ;
and
  return Math.ceil( val * 0.5d ) / 0.5f ;
0
 
zzynxSoftware engineerCommented:
Stupid me. That's wrong sorry.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
CEHJConnect With a Mentor Commented:
(that's for nearest .25) - change to 2.0 for nearest .5
0
 
CEHJCommented:
... and of course 'd' is the original value
0
 
ycompAuthor Commented:
thanks CEHJ
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.