Java .floor()
Published Oct 31, 2022Updated Nov 10, 2022
The Math.floor() method returns the largest integer value that is less than or equal to the argument.
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:
numis already an integer.numisNaN, 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.javapublic 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.01.00.0
Example 2
The following example features unsuccessful calls to the Math.floor() method:
// Example.javapublic 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:
NaNInfinity
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