'Math.ceil for positive value greater than zero? [duplicate]
The following code is giving me a wrong answer and I can't understand why.
int x = 10;
int y = 15;
System.out.println((int)Math.ceil(x/y));
Result:
0
I was expecting a 1 as the answer.
Solution 1:[1]
Inside the Math.ceil function you passed x / y and the answer of x / y in your case is gonna be 0 cause x and y are both integers and in Java, dividing two integers is going to give an integer result, thus 10 / 15 = 0 without taking the decimal digits. You can try defining x or y as double. Then you will get 10 / 15 = 0.666... and the ceil will return a 1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | salam |