AppDividend
Latest Code Tutorials

Java Math floor() Function Example

The java.lang.Math.floor() method is an inbuilt method that is used to calculate the nearest integer less than (or equal to, if the passed value is itself an integer) the passed value. Floor values are often required in various algorithm-building scenarios dealing with mathematical operations and can be directly seen in different mathematical formulae as well.

Java Math floor()

Java Math.floor(double a) method returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer. If the argument value is already equal to a mathematical integer, then the result is the same as the argument.

Syntax

```public static double floor(double x)
```

Parameter(s)

The variable of the double type whose floor value is to be calculated.

Return Value

For integral values, returns the passed value.

For non-integral values, returns the nearest integer less than the passed value.

Note

1. If the passed value is NaN, infinite, positive zero, or negative zero, then the method returns the same value that is passed.
2. For positive values less than 1.0, this method returns positive zero.
3. Math.ceil(x) = – Math.floor(-x).

Consider the following examples.

Example1.java: The following example demonstrates the use of this method.

```import java.lang.Math;

public class Example1 {
public static void main(String[] args) {
double a = 1.3;
double b = 1.0;

System.out.println(Math.floor(a));
System.out.println(Math.floor(b));
}
}
```

Output

```->javac Example1.java
->java Example1
1.0
1.0
```

Example2.java: The following example demonstrates the situation when a negative value is passed.

```import java.lang.Math;

public class Example2 {
public static void main(String[] args) {
double a = -1.3;
System.out.println(Math.floor(a));

}
}
```

Output

```->javac Example2.java
->java Example2
-2.0

```

Example3.java: The following example demonstrates the situation of passing NaN, infinite, positive zero, or negative zero values.

```import java.lang.Math;

public class Example3 {
public static void main(String[] args) {
double a = Double.NaN;
double b = Double.POSITIVE_INFINITY;
double c = Double.NEGATIVE_INFINITY;
double d = 0.0;
double e = -0.0;

System.out.println(Math.floor(a));
System.out.println(Math.floor(b));
System.out.println(Math.floor(c));
System.out.println(Math.floor(d));
System.out.println(Math.floor(e));
}
}
```

Output

```->javac Example3.java
->java Example3
NaN
Infinity
-Infinity
0.0
-0.0
```

Example4.java: The following example demonstrates the situation of passing a positive value of less than 1.0

```import java.lang.Math;

public class Example4 {
public static void main(String[] args) {
double a = 0.3;
System.out.println(Math.floor(a));
}
}
```

Output

```->javac Example4.java
->java Example4
0.0
```

Example5.java: The following example demonstrates the equality of Math.ceil(x) and negative of Math.floor(-x).

```import java.lang.Math;

public class Example5 {
public static void main(String[] args) {
double a = 2.4;
System.out.println(Math.ceil(a));
System.out.println(-Math.floor(-a));
}
}
```

Output

```->javac Example5.java
->java Example5
3.0
3.0
```

Example6.java: The following example demonstrates a scenario where the floor method can be used. Consider the following problem.

```import java.util.*;

public class Example6{
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
double budget = sc.nextDouble();
double salary = sc.nextDouble();
System.out.println(Math.floor(budget/salary));
}
}
```

Output

```Test Case 1:
Input :
200000
26000
Output: 7.0

Test Case 2:
Input :
250000
100000

Output: 2.0
```