The Math.floor() is a built-in static method of the Math class which is imported from java.lang package. It returns a double value that is less than or equal to the specified number. The Math.floor() method is of significant importance in areas where concrete values are required. For instance, the Math.floor() method rounds down the 5.5 to 5.0 and returns the value.
Quick Outline
- How to Use the Java Math.floor() Method?
- What are the Boundary Cases for the Math.floor() Method?
- Math.floor() vs Math.ceil() vs Math.round(): Key Differences
- Bonus Tip: Additional Floor Functions
- Final Thoughts
How to Use the Java Math.floor() Method?
The Math.floor() method accepts a single parameter for “val1”. The datatype of this parameter can be double, float, int, or long. However, the output returned is a double value. The basic syntax of the Math.floor() is given as follows:
public static double Math.floor(datatype val1)
Below are various examples of the Math.floor() method in Java:
Example 1: Basic Use of the Math.floor() Method
The values greater than 5 after the decimal point are rounded off and added to the number on the left according to the standard practice. However, the Java Math.floor() returns a double value that is less or equal to the specified value as shown in the following code:
System.out.println("Floor value is " + Math.floor( 12.2 )); //expected output : 12.0
System.out.println("Floor value is " + Math.floor(12.5)); //expected output : 12.0
System.out.println("Floor value is " + Math.floor(12.9)); //expected output : 12.0
Complete Code & Output
Example 2: Use of the Math.floor() Method With Different Data Types
Within this example, the Math.floor() method takes double, float( 6.8f ), int, and long value (6l) as parameters and calculates the floor value:
System.out.println("Floor value of the Double Number is " + Math.floor( 5.7 ));
System.out.println("Floor value of the Float Number is " + Math.floor( 6.8f ));
System.out.println("Floor value of an Integer Number is " + Math.floor( 9 ));
System.out.println("Floor value of the Long Number is " + Math.floor( 6l ));
Complete Code & Output
Example 3: Use of the Math.floor() Method With Negative and Positive Values
The floor() functions return a value of lesser magnitude when calculating the floor value of a negative number e.g., -13 is returned for -12.8. Within this example, the floor value of positive and negative values is calculated using the Math.floor() function. The given line of code imports the Scanner class for taking the user input:
import java.util.Scanner; // imports the Scanner Class
Create a Scanner object “inputObj” and prompt the user to enter the positive and negative numbers. The values are stored in the double-type variables named “positiveValue” and “negativeValue”:
Scanner inputObj=new Scanner(System.in); //Enter input using keyboard
System.out.println("Enter a positive and negative number");
double positiveValue=inputObj.nextDouble();
double negativeValue=inputObj.nextDouble();
The Math.floor() returns the double floor value for the given values and prints the output:
System.out.println("For Positive Numbers " + Math.floor(positiveValue));
System.out.println("For Negative Numbers " + Math.floor(negativeValue));
Complete Code & Output
Example 4: Use of the Math.floor() Method With Arrays
In this example, the Math.floor() method calculates the floor value for multiple elements of the array. For this purpose, declare and initialize a double array “numArray” with decimal values:
double[] numArray= {9.8, 10.2, 11.5, 14.7, 99.9};
A for loop iterates over the elements of the array using the integer variable “i”. The “i” variable is initialized with 0 and is incremented by 1 in each iteration. The loop terminates when the value of i exceeds numArray length. Within the print statement, the Math.floor() method calculates the floor value for each element of the array:
for (int i=0; i<numArray.length; i++)
{
System.out.println("Output : " + Math.floor(numArray[i]));
}
Complete Code & Output
Example 5: Use of the Math.floor() Method With Expressions and Square Root
In this example, the floor value is calculated for the given expressions using the Math.floor() method. The Math.sqrt() function will return the square root of 5 and then the floor value of it will be calculated:
System.out.println("Floor value for 2*4 is = " + Math.floor( 2 * 4));
System.out.println("Floor value for 3+8 /5 is = " + Math.floor(3+8 / 5));
System.out.println("Floor value for sqrt(5) is = " + Math.floor(Math.sqrt(5)));
Complete Code & Output
What are the Boundary Cases for the Math.floor() Method?
Boundary cases for the Math.floor() method are those cases for which no floor value is calculated. The argument passed is returned as it is in the output. The following are the boundary cases for the Math.floor() method:
- The integer value passed to the Math.floor() method is returned as it is in the double format in output.
- If the arguments passed to the Math.floor() method are positive or negative infinity, the arguments are returned as it is in the output.
- No floor value is calculated for the negative square root values. The method returns NaN in the output.
Given below is a practical demonstration of the Java Math.floor() function:
Case 1: Use the Math.floor() Method With Integer Values
The Math.floor() method calculates the floor value for the 0, 9, and an additive expression and returns the output in the double format:
System.out.println("Floor value of 0 is = " + Math.floor(0));
System.out.println("Floor value of 9 is = " + Math.floor(9));
System.out.println("Floor value of 3+2 is = " + Math.floor(3+2));
Complete Code & Output
Case 2: Use the Math.floor() Method With Positive and Negative Infinity
The 32-bit float and 64-bit double in Java supports Infinity as it cannot be accommodated in the integer datatype (real numbers). In this example, the Math.floor() method takes the positive infinity of double type as an argument and calculates the floor value. Another method of calculating the positive infinity is dividing “1.0” by “0”:
System.out.println("Positive Infinity floor value = " + Math.floor(Double.POSITIVE_INFINITY));
System.out.println("Floor value of expression = " + Math.floor(1.0/0));
Similarly, the negative infinity of double type is passed as an argument to the Math.floor() function. Dividing “-1.0” by “0” also yields the same results as shown in the code below:
System.out.println("Negative Infinity floor value = " + Math.floor(Double.NEGATIVE_INFINITY));
System.out.println("Floor value of expression = " + Math.floor(-1.0/0));
Complete Code & Output
Case 3: Use the Math.floor() Method With NaN
In Java, the NaN stands for “Not a number”. The Math.floor() method returns NaN when taking the square root of a negative number or invoking it by using the double data type as shown below:
System.out.println("Floor Value is = " + Math.floor(Math.sqrt(-1)));
System.out.println("Floor Value is = " + Math.floor(Double.NaN));
Complete Code & Output
Math.floor() vs Math.ceil() vs Math.round(): Key Differences
The following table distinguishes between the three methods of Math class i.e., Math.floor(), Math.ceil(), and Math.round():
Math.floor() | Math.ceil() | Math.round() |
---|---|---|
The Math.floor() method rounds down the number to a double value that is less or equal to the given number. | The Math.ceil() method rounds up the number to a double value which is greater or equal to the given number. | The Math.round() method rounds off the number to a double value which is less than or greater than the given number. |
Example: The Math.floor() returns 19.0 which is a rounded-down value for 19.4. | Example: The Math.ceil() returns 20.0 which is rounded up value for 19.4. | Example: The Math.round() returns 19 which is the closest value for 19.4. |
Comparison of the Three Methods:
The following code presents the comparison of the Math.floor(), Math.ceil(), and Math.round() methods in Java:
Bonus Tip: Additional Floor Functions of Math Class
Besides using Math.floor() method in Java, you can also use other floor functions like floorDiv() and floorMod(), which are discussed below:
Math.floorDiv(): This method inputs two float, double, int, or long values, and divides the two numbers.
Math.floorMod(): This method takes two arguments of float, double, int, or long values, and returns the remainder by dividing the two values.
Sample Code & Output
That is all from this section of the article.
Final Thoughts
The floor() method in Java is a built-in static method of Math class that rounds down the value to a number that is equal to or less than the given value. It accepts input in double, float, int, or long, and returns a double value as output. The Math.floor() function does not calculate floor values for the NaN, positive or negative infinity. This article has discussed and demonstrated various examples of the Math.floor() method in Java. You can understand the working and implementation of the Math.floor() method in Java by following this article.