Java .floor()

J0hnRJr's avatar
Published Oct 31, 2022Updated Nov 10, 2022
Contribute to Docs

The Math.floor() method returns the largest integer value that is less than or equal to the argument.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours

Syntax

Math.floor(x);

The num parameter can either be a floating-point value or a variable that points to a floating-point value. The return value is the same as num under the following conditions:

  • num is already an integer.
  • num is NaN, infinity, or zero (positive or negative).

Example 1

In the case of double values, Math.floor() returns the next integer value below the provided double value. In the case of integers provided, these will return the same value as provided:

// Example.java
public class Example {
public static void main(String args[]) {
System.out.println(Math.floor(-3.9));
System.out.println(Math.floor(1.0001));
System.out.println(Math.floor(0));
}
}

This will produce the following output:

-4.0
1.0
0.0

Example 2

The following example features unsuccessful calls to the Math.floor() method:

// Example.java
public class Example {
public static void main(String args[]) {
System.out.println(Math.floor(2.0 % 0));
System.out.println(Math.floor(Double.POSITIVE_INFINITY));
}
}

This will produce the following output:

NaN
Infinity

All contributors

Contribute to Docs

Learn Java on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.
    • Beginner Friendly.
      17 hours