BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Math.ceil() Method

By Chaitanya Singh | Filed Under: java

Java Math.ceil() method returns the closest integer value, which is greater than or equal to the given value. For example, Math.ceil(9.9) would return 10. In this guide, we will discuss the ceil() method with examples.

public class JavaExample
{
  public static void main(String[] args)
  {
    double n1 = 5.55, n2 = -5.55;
    System.out.println(Math.ceil(n1));
    System.out.println(Math.ceil(n2));
  }
}

Output:

6.0
-5.0

Syntax of ceil() method

Math.ceil(9.9); //returns 10

ceil() Description

public static double ceil(double num): Returns a nearest integer value to the passed value num.

ceil() Parameters

It takes a single parameter:

  • num: A double value, whose closest integer to be determined.

ceil() Return Values

  • If the given value is an integer, then it returns the same value.
  • If the given value is NaN (Not a number) or an infinity or positive zero or negative zero, then the result is the same as the given value.
  • If the given value is less than 0 but greater than -1.0, then negative zero is returned.

Example 1: Ceil value for positive and negative number

public class JavaExample
{
  public static void main(String[] args)
  {
    double n1 = 19.1, n2 = -19.1;
    System.out.println(Math.ceil(n1));
    System.out.println(Math.ceil(n2));
  }
}

Output:

Java Math.ceil() Example Output_1

Example 2: Ceil value for a number greater than -1 and less than 0

public class JavaExample
{
  public static void main(String[] args)
  {
    // -1 < n < 0
    double n = -.15;
    System.out.println(Math.ceil(n));
  }
}

Output:

Java Math.ceil() Example Output_2

Example 3: Ceil value for positive and negative infinity

public class JavaExample
{
  public static void main(String[] args)
  {
    double n1 = 5.0/0; //positive infinity
    double n2 = -5.0/0; //negative infinity
    System.out.println(Math.ceil(n1));
    System.out.println(Math.ceil(n2));
  }
}

Output:

Java Math.ceil() Example Output_3

Example 4: Ceil for NaN(Not a Number)

public class JavaExample
{
  public static void main(String[] args)
  {
    double n = 0.0/0; //NaN
    System.out.println("Ceil for NaN: "+Math.ceil(n));
  }
}

Output:

Java Math.ceil() Example Output_4

Recommended Posts

  • Java Math.signum()
  • Java Math.pow()
  • Java Math.cbrt()
  • Java Math.sqrt()
❮ Java Math

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap