# Java floor Function

The Java Floor Function is one of the math or Mathematical Functions available in the 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 values 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 the floor value.

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

## Java math Floor Function Example

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

```// Example
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);
}
}```

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) ==> 150.9653 ==> 150

Next, we used the 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 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));```

## floor on Array example

In this math 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 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("%.2f\n", Math.floor(myArray[i]));
}
}
}```
``````-16.00
45.00
44.00
-16.00
-7.00
3.00``````

We used the 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 static double floor(double number) method to find the corresponding closet (floor) values and print the output.

`System.out.format("%.2f\n", Math.floor(myArray[i]));`

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

## Java math floor function on Arraylist example

In this Java program, we are going to declare an ArrayList of double type and find the closest 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);

for (double x : myList) {
System.out.println(Math.floor(x));
}
}
}```
``````-11.0
-11.0
15.0
40.0
-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 it directly inside the System.out.format statement. Here, the compiler will call the math method ( static double floor(double x) ) to find the corresponding closet values, and print the output.

`System.out.println(Math.floor(x));`
Back to Categories Java