LorenzoBerti LorenzoBerti - 1 month ago 10
Android Question

Round up float .25

Hello I should round up my float value to successive .25 value.
Example:

0.123 => 0.25
0.27 => 0.5
0.23 => 0.25
0.78 => 1
0.73 => 0.75
10.20 => 10.25
10.28 => 10.5


I tried use
Math.round(myFloat*4)/4f;
but it return the nearest, so if I have:

1.1 return 1 and not 1.25

Answer

You should be using Math.ceil() instead of Math.round():

Math.ceil(myFloat*4) / 4.0d;

Closely related: Java - rounding by quarter intervals