Java Math floorDiv() Method
Last Updated :
13 May, 2025
The Math.floorDiv() is a built-in math function in Java that returns the largest (closest to negative infinity) integer value that is less than or equal to the algebraic quotient of two numbers. As floorDiv() is static, object creation is not required. The floorDiv() method performs division that rounds towards negative infinity.
Syntax of floorDiv() Method
public static int floorDiv(int x, int y)
public static long floorDiv(long x, long y)
Parameters:
- x: This is the number to be divided i.e., dividend.
- y: This is the number by which the dividend is divided i.e., divisor.
The method accepts both int and long data types for the parameters.
Return Value: This method returns the largest integer value that is less than or equal to the algebraic quotient of the division of x by y.
Exception: The method throws an ArithmeticException if the divisor is 0.
Examples of Java Math floorDiv() Method
Example 1: In this example, we will see how Math.floorDiv() works with both positive numbers and how it differs from the standard division operator.
Java
// Java program to demonstrate the use of Math.floorDiv()
import java.lang.Math;
public class Geeks {
public static void main(String[] args) {
int a = 25, b = 5;
System.out.println("The result is: " + Math.floorDiv(a, b));
// Example with non-divisible result
int c = 125, d = 50;
System.out.println("The result is: " + Math.floorDiv(c, d));
}
}
OutputThe result is: 5
The result is: 2
Explanation:
- In the first case, 25 / 5 results in 5. The quotient is already an integer, so, the result of Math.floorDiv(25, 5) is also 5.
- In the second case, 125 / 50 results in 2.5. So, the method rounds the result down to the nearest integer less than or equal to the quotient. So, it gives 2 i.e., the result that is less than or equal to the quotient.
Example 2: In this example, we will see what happens when we attempt to divide by zero using Math.floorDiv().
Java
// Java program to demonstrate ArithmeticException
import java.lang.Math;
public class Geeks {
public static void main(String[] args) {
int x = 20, y = 0;
System.out.println("The result is: " + Math.floorDiv(x, y));
}
}
Output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
When to Use Math.floorDiv() Instead of the Standard Division (/)
We should use Math.floorDiv() when:
- We need consistent rounding down behavior, when working with negative numbers.
- We want the result to follow the "floor" approach, rounding towards negative infinity, rather than truncating the decimal part which is what the standard division operator / does.
- We are working with modular arithmetic, where we need to ensure results conform to specific constraints such as rounding downward.
Similar Reads
Math floorDiv() method in Java The Math.floorDiv() is a built-in math function in Java that returns the largest (closest to negative infinity) integer value that is less than or equal to the algebraic quotient of two numbers. As floorDiv() is static, object creation is not required. The floorDiv() method performs division that ro
3 min read
Math floorMod() Method in Java The Math.floorMod() is a built-in math function in Java that returns the floor modulus of the integer arguments passed to it. Therefore, floor modulus is (a - (floorDiv(a, b) * b)), has the same sign as the divisor b, and is in the range of -abs(b) < result < +abs(b). The relationship between
3 min read
Java Math decrementExact() method The java.strictmath.lang.decrementExact() is a built-in function in java which returns the argument decremented by one, throwing an exception if the result overflows the specified datatype either long or int depending on which data type has been used on the method argument. Since this is decrement,
2 min read
NavigableSet floor() method in Java The floor() method of NavigableSet interface in Java is used to return the greatest element in this set less than or equal to the given element, or null if there is no such element exists in the set. Syntax: E floor(E ele) Where, E is the type of elements maintained by this Set container. Parameters
2 min read
Java floor() method with Examples The java.lang.Math.floor() returns the double value that is less than or equal to the argument and is equal to the nearest mathematical integer. Note: If the argument is Integer, then the result is Integer.If the argument is NaN or an infinity or positive zero or negative zero, then the result is th
2 min read
NavigableMap floorKey() method in Java The floorKey() method of NavigableMap interface in Java is used to return the greatest key less than or equal to the given key, or null if there is no such key. Syntax: K floorKey(K key) Where, K is the type of key maintained by this map. Parameters: This function accepts a single parameter Key whic
2 min read