CodeGym /Java Blog /Java Numbers /Java floor() method
Author
John Selawsky
Senior Java Developer and Tutor at LearningTree

Java floor() method

Published in the Java Numbers group

What is floor function in mathematics?

A floor function which is also known as the greatest integer function in maths takes a real number “x” as an input. It returns the greatest integer that is less than or equal to input number x. It is normally denoted as floor(x) or ⌊x⌋. It is used to convert a real number having fractional part into an integer without the fractional part. To understand it better let's have a quick look at the examples below.

floor(5) = 5
floor (1.3) = 1
floor (7.9) = 7

What is Math.floor() method in Java?

Java provides an equivalent of mathematical floor function. Here’s how you can understand it.
Math.floor() method in Java returns a “double” value equal to the greatest integer less than or equal to the argument.
If the given number is already an integer it returns the integer. If the arguments is zero, infinity or a NaN it returns the same argument.

Method Header


public static double floor(double x)
The method takes a double value (double x) as a parameter whose floor has to be determined. It does not require to import any external package.

Return Type math.floor

The method returns a double (double floor) value which is less than or equal to the given parameter.

Example


public class Driver1 {

	public static void main(String[] args) {


		double x = 50; // floor for whole number (Integer value)
 		double floorValue = Math.floor(x);
		System.out.println("floor⌊" + x + "⌋ = " + floorValue);

		x = 21.7; // floor for positive decimal
		floorValue = Math.floor(x);
		System.out.println("floor⌊" + x + "⌋ = " + floorValue);

		x = -21.7; // floor for negative decimal
		floorValue = Math.floor(x);
		System.out.println("floor⌊" + x + "⌋ = " + floorValue);
	
		x = 0; // floor for zero (Integer value)
		floorValue = Math.floor(x);
		System.out.println("floor⌊" + x + "⌋ = " + floorValue);
		
		
		// Boundary Cases 
		x = +3.3/0;  // Case I - floor for +Infinity
		floorValue = Math.floor(x);
		System.out.println("floor⌊" + x + "⌋ = " + floorValue);
		
		x = -3.3/0; // Case II - floor for -infinity
		floorValue = Math.floor(x);
		System.out.println("floor⌊" + x + "⌋ = " + floorValue);
		
		x = -0.0/0; // Case III - floor for NaN
		floorValue = Math.floor(x);
		System.out.println("floor⌊" + x + "⌋ = " + floorValue);
	
	}

}

Output

floor⌊50.0⌋ = 50.0 floor⌊21.7⌋ = 21.0 floor⌊-21.7⌋ = -22.0 floor⌊0.0⌋ = 0.0 floor⌊Infinity⌋ = Infinity floor⌊-Infinity⌋ = -Infinity floor⌊NaN⌋ = NaN

Explanation

In the code snippet above, we have used different input values to the floor function to determine its output. We have used both positive and negative real numbers as an input value. We have also passed positive and negative infinity along with a Nan and zero value to check the results of the floor function.

Conclusion

So that was the basic implementation of the Math.floor(x) method in Java. Don’t forget to practise as you learn. Feel free to post any questions you might have. Happy learning!
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION