Java Floor Function

The Java Floor Function is one of the math or Mathematical Functions available in Java Math Library. This Java math floor function is to return the closest double value, which is less than or equal to the specified expression or Value and equal to a mathematical integer.

Java Floor Function Syntax

The basic syntax of the Math.floor in Java Programming language is as shown below. The following function will accept positive or negative double value as an argument. It returns the closest double value, which is less than or equal to the specified expression or Value and equal to a mathematical integer.

static double floor(double number); //Return Type is Double
// In order to use in program: 
Math.floor(double number);

Number: It can be a double value or a valid numerical expression on which you want to find floor value.

  • If the number argument is a positive or negative number, the Java Math.floor function will return the floor value.
  • If the number argument is positive or negative zero, the Math.floor function will return the same argument.
  • And if the number argument is not a number (NaN), the Math.floor function will return the same argument.

Java Floor Function Example

The Java floor Function returns the closest integer value, which is less than or equal to given numeric value. The following query will show multiple ways to use math.floor in java.

// Java Floor Function
package MathFunctions;

public class FloorMethod {
	public static void main(String[] args) {
		double a = Math.floor(10.9666 - 14.9865 + 154.9852);

		System.out.println("Floor value of Positive Number: " + Math.floor(10.25));
		System.out.println("Floor value of Positive Number: " + Math.floor(10.95));
		
		System.out.println("\nFloor value of Negative Number: " + Math.floor(-20.85));
		System.out.println("Floor value of Negative Number: " + Math.floor(-20.25));
		
		System.out.println("\nFloor value = " + a);	
	}
}
Java Floor Function 1

First, we declared a variable of type Double and performed the Math.floor function directly on expression.

double a = Math.floor(10.9666 - 14.9865 + 154.9852);

Math.floor(10.9666 – 14.9865 + 154.9852)

==> Math.floor (150.9653) ==> 150

Next, we used the Math.floor Function directly on Positive double values. If you observe the below code snippet, though 10.95 is near to 11, the java math.floor is returning 10 as output. It is because 11 will be greater than the given argument.

System.out.println("Floor value of Positive Number: " + Math.floor(10.25));
System.out.println("Floor value of Positive Number: " + Math.floor(10.95));

Next, we used the floor Math function directly on Negative double values.

System.out.println("\nFloor value of Negative Number: " + Math.floor(-20.85));
System.out.println("Floor value of Negative Number: " + Math.floor(-20.25));

Java floor on Array example

In this math floor program, we will show how to find the floor values of bulk data. Here, we are going to declare an array of double type and find the closest (floor) values of array elements.

package MathFunctions;

public class FloorMethodOnArray {
	public static void main(String[] args) {
		
		double [] myArray = {-15.69, 45.98, 44.21, -15.9999, -6.879, 3.4897};

		for (int i = 0; i < myArray.length; i++) {
			System.out.format("Floor value of Array Element = %.2f\n", Math.floor(myArray[i]));
		}
	}
}
Floor value of Array Element = -16.00
Floor value of Array Element = 45.00
Floor value of Array Element = 44.00
Floor value of Array Element = -16.00
Floor value of Array Element = -7.00
Floor value of Array Element = 3.00

We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, the compiler will check for the condition (i < myArray.length). 

TIP: myArray.length finds the length of the Java array.

for (int i = 0; i < myArray.length; i++) {

The following statement used the Math.floor Function directly inside the System.out.format statement. Here, it will call the floor method ( static double floor(double number) ) to find the corresponding closet (floor) values, and prints the output.

System.out.format("Floor value of Array Element = %.2f\n", Math.floor(myArray[i]));

NOTE: To find the closet (floor) value of a single item, then use: Math.floor(myArray[index_position])

Java floor function on Arraylist example

In this Java program, we are going to declare an ArrayList of double type and find the closet values of list elements.

package MathFunctions;

import java.util.ArrayList;

public class FloorMethodOnList {
		public static void main(String[] args) {
			
			ArrayList<Double> myList = new ArrayList<Double>(5);
			myList.add(-10.789);
			myList.add(-10.65);
			myList.add(15.9876);
			myList.add(40.9958979);
			myList.add(-44.4589);
			
			for (double x : myList) {
				System.out.println("Floor value of ArrayList =  " + Math.floor(x));
			}
		}
	}
Floor value of ArrayList =  -11.0
Floor value of ArrayList =  -11.0
Floor value of ArrayList =  15.0
Floor value of ArrayList =  40.0
Floor value of ArrayList =  -45.0

In this math floor function example, we used the For Loop to iterate the double values in ArrayList

for (double x : myList) {

We used the Math.floor Function directly inside the System.out.format statement. Here, the compiler will call the java math.floor method ( static double floor(double x) ) to find the corresponding closet (floor) values, and print the output.

System.out.println("Floor value of ArrayList =  " + Math.floor(x));