I don’t know why, but I’m getting two different result if I’m changing the parameter from a decimal to a fraction.
These methods would return the exact value. I’m trying to round up a number if it’s a decimal, for example:
0.0 -> 0
0.1 -> 1
0.4 -> 1
0.5 -> 1
0.6 -> 1
1.0 -> 1
1.1 -> 2
// accepts Double
private void myRound(Double d){
int res = (int)Math.ceil(d);
return (res <= 0 ? 1 : res);
}
// acepts int
private void myRound(int i){
int res = (int)Math.ceil(i);
return (res <= 0 ? 1 : res);
}
Example:
System.out.println(myRound(14 / 10));
OUTPUT: 1
System.out.println(myRound(1.4);
OUTPUT: 2
The thing is that firstly the conversion goes to Integer, where (14/10) is 1, and then it ceils it to 1. 1.4 is a double, so it makes it ceil as a double number.