- 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

25th Oct 2016, 9:56 AM
Celin
2 Réponses
+ 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!
6th Nov 2016, 1:29 PM
Nikhil Kolachalama
Nikhil Kolachalama - avatar
0
Math.ceil returns a double.
25th Oct 2016, 10:01 AM
Zen
Zen - avatar