![](./write-blog/images/933669955-9-floor-Method.png)
Java floor() Method: Usage and Examples
Table of Content:
Description
This java tutorial tells how to use the floor(double) method of Math class under java.lang package. This 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
The method floor gives the largest integer that is less than or equal to the argument.
Syntax
This method has the following variants ?
double floor(double d) double floor(float f)
Parameters
Here is the detail of parameters ?
- A double or float primitive data type.
Return Value
The floor(double a) method simply returns the largest double value that is less than or equal to the argument and is equal to an integer. Special cases:
- If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
- If the argument is NaN or an infinity or positive zero or negative zero, then the result is the same as the argument.
This method returns the largest integer that is less than or equal to the argument. Returned as a double.
Example
public class FloorMethod { public static void main(String args[]) { double d = -120.675; float f = -70; System.out.println(Math.floor(d)); System.out.println(Math.floor(f)); System.out.println(Math.ceil(d)); System.out.println(Math.ceil(f)); } }
Output
-121.0 -70.0 -120.0 -70.0 Press any key to continue . . .
Example
This java example source code demonstrates the use of floor(double a) method of Math class. Basically we just get the floor value of a double variable coming from the user input.
import static java.lang.System.*; import java.util.Scanner; /* * This example source code demonstrates the use of * floor(double a) method of Math class * Get the floor value of the user input */ public class MathFloor { public static void main(String[] args) { // ask for user input out.print("Enter a value:"); Scanner scan = new Scanner(System.in); // use scanner to get user console input double value = scan.nextDouble(); // get the ceiling of a value double floorValue = Math.floor(value); out.println("floor of "+value+" = "+floorValue); // close the scanner object to avoid memory leak scan.close(); } }
Output
Running the abs(double a) method example source code of Math class will give you the following outputEnter a value:156.235 floor of 156.235 = 156.0 Press any key to continue . . .
Find floor value of a number using Math.floor
/* Find floor value of a number using Math.floor This java example shows how to find a floor value of a number using floor method of Java Math class. floor method returns the largest interger which is not grater than the value. */ public class FindFloorValueExample { public static void main(String[] args) { /* * To find a floor value, use * static double floor(double d) method of Math class. * * It returns a largest integer which is not grater than the argument value. */ //Returns the same value System.out.println(Math.floor(70)); //returns largest integer which is not less than 30.1, i.e. 30 System.out.println(Math.floor(30.1)); //returns largest integer which is not less than 15.5, i.e. 15 System.out.println(Math.floor(15.5)); //in this case it would be -40 System.out.println(Math.floor(-40)); //it returns -43. System.out.println(Math.floor(-42.4)); //returns 0 System.out.println(Math.floor(0)); } }
Output
70.0 30.0 15.0 -40.0 -43.0 0.0 Press any key to continue . . .