Skip to Content

Find ceiling value of a number using Math.ceil

  1. /*
  2.   Find ceiling value of a number using Math.ceil
  3.   This java example shows how to find a ceiling value of a number using ceil method
  4.   of Java Math class. ceil method returns the smallest interger which is not
  5.   less than the value.
  6. */
  7.  
  8. public class FindCeilingExample {
  9.  
  10.   public static void main(String[] args) {
  11.    
  12.     /*
  13.      * To find a ceiling value, use
  14.      * static double ceil(double d) method of Math class.
  15.      *
  16.      * It returns a smallest integer which is not less than the argument value.
  17.      */
  18.    
  19.     //Returns the same value
  20.     System.out.println(Math.ceil(10));
  21.    
  22.     //returns a smallest integer which is not less than 10.1, i.e. 11
  23.     System.out.println(Math.ceil(10.1));
  24.    
  25.     //returns a smallest integer which is not less than 5.5, i.e. 6
  26.     System.out.println(Math.ceil(5.5));
  27.    
  28.     //in this case it would be -20
  29.     System.out.println(Math.ceil(-20));
  30.    
  31.     //it returns -42 not -43. (-42 is grater than 42.4 :) )
  32.     System.out.println(Math.ceil(-42.4));
  33.    
  34.     //returns 0
  35.     System.out.println(Math.ceil(0));
  36.    
  37.   }
  38. }
  39.  
  40. /*
  41. Output would be
  42. 10.0
  43. 11.0
  44. 6.0
  45. -20.0
  46. -42.0
  47. 0.0
  48. */