Java ceil() method with Examples

Java 8Object Oriented ProgrammingProgramming

<p>The&nbsp;java.lang.Math.ceil()&nbsp;returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer. Special cases &minus;</p><ul class="list"><li><p>If the argument value is already equal to a mathematical integer, then the result is the same as the argument.</p></li><li><p>If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.</p></li><li><p>If the argument value is less than zero but greater than -1.0, then the result is negative zero.</p></li></ul><h2>Example</h2><p>Following is an example to implement the ceil() method in Java &minus;</p><pre class="prettyprint notranslate">import java.lang.*; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; // get two double numbers &nbsp; &nbsp; &nbsp; double x = 125.9; &nbsp; &nbsp; &nbsp; double y = 0.4873; &nbsp; &nbsp; &nbsp; // call ceal for these these numbers &nbsp; &nbsp; &nbsp; System.out.println(&quot;Math.ceil(&quot; + x + &quot;)=&quot; + Math.ceil(x)); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Math.ceil(&quot; + y + &quot;)=&quot; + Math.ceil(y)); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Math.ceil(-0.65)=&quot; + Math.ceil(-0.65)); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">Math.ceil(125.9)=126.0 Math.ceil(0.4873)=1.0 Math.ceil(-0.65)=-0.0</pre><h2>Example</h2><p>Let us see another example with negative input values &minus;</p><pre class="prettyprint notranslate">Import java.lang.*; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; // get two double numbers &nbsp; &nbsp; &nbsp; double x = 0.20; &nbsp; &nbsp; &nbsp; double y = -2.4; &nbsp; &nbsp; &nbsp; // call ceal for these these numbers &nbsp; &nbsp; &nbsp; System.out.println(&quot;Math.ceil(&quot; + x + &quot;)=&quot; + Math.ceil(x)); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Math.ceil(&quot; + y + &quot;)=&quot; + Math.ceil(y)); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">Math.ceil(0.2)=1.0 Math.ceil(-2.4)=-2.0</pre>
raja
Updated on 24-Sep-2019 08:27:53

Advertisements