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. */

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!