- 1
In the Math class , Math.ceil() will always return an integer value then why is the following code giving an error ?
public class Program { public static void main(String[] args) { int c = Math.ceil(7.342); System.out.println(c); } } Output: ..\Playground\:4: error: incompatible types: possible lossy conversion from double to int int c = Math.ceil(7.342); ^ 1 error
2 ответов
+ 1
Math.ceil(<some value>) returns a double, not an integer. If you would like to have the ceiling value be an integer, you can cast the expression:
int c = (int) (Math.ceil(7.342));
This casting truncates the .0 at the end of the ceiling value
Originally, it would have been 8.0; after casting it as an integer, the ceiling became 8
Hope that helps!
0
Math.ceil returns a double.