# take a double, then round decimal to either to .0 or .5 only

• September 30th, 2011, 03:11 PM
MaxBodine
take a double, then round decimal to either to .0 or .5 only
Hi,

I have formated a double like this 9.255 will display 9.3

public double rdTwoDecimal(double d)
{
DecimalFormat twoD = new DecimalFormat("#.0");
return Double.valueOf(twoD.format(d));
}

What I now need to do is the decimal can only be .0 or .5 >>>> so 9.3 would be 9.5 when rounded.

I need to figure out how to get the 3, so I can check if it is 3,4,6,7 then display 5 maybe?

• September 30th, 2011, 03:21 PM
Sean4u
Re: take a double, then round decimal to either to .0 or .5 only
Quote:

9.3 would be 9.5 when rounded.
Do you want to split on 0.3, or do you also want 0.25 to round up to 0.5? If the latter, you may get the right answer if you double the figure first, then round, then halve.
• September 30th, 2011, 03:25 PM
MaxBodine
Re: take a double, then round decimal to either to .0 or .5 only
I would want .25 to be .5 too
• September 30th, 2011, 03:35 PM
kenster421
Re: take a double, then round decimal to either to .0 or .5 only
Code Java:

```//lets say some double value = 9.3 double cutOff = 0.5; if(value < (double)((int)(value))+cutOff)) //change this expression to validate your range x.0<=value<x.5 will round down to x.0 value = (double)((int)(value)); else //x.5<=value<(x+1).0 will round down to x.5 value = (double)((int)(value)) +.5```