It seems like a simple question, but I really suck in math , and the few online examples that I was looking for do not seem to work for me. (the result returns only the same value as the input, etc.)
For example .. but its in C not Java Round to Next.05 in C
So my goal: I have %.1f format float or double or big decimal and you want to round it to the nearest .5
example: 1.3 --> 1.5 5.5 --> 5.5 2.4 --> 2.5 3.6 --> 4.0 7.9 --> 8.0
I tried the following example, but did not work :( below is only output 1.3, which is the original value. I wanted it to be 1.5
public class tmp { public static void main(String[] args) { double foo = 1.3; double mid = 20 * foo; System.out.println("mid " + mid); double out = Math.ceil(mid); System.out.println("out after ceil " + out); System.out.printf("%.1f\n", out/20.0); } }
java rounding
masato-san
source share