Approximate numbers to the closest smaller integer.
System.out.println(Math.floor(0.60)); System.out.println(Math.floor(0.40)); System.out.println(Math.floor(5)); System.out.println(Math.floor(5.1)); System.out.println(Math.floor(-5.1)); System.out.println(Math.floor(-5.9)); |
The floor() method rounds a number downwards to the nearest integer.
Tip: For rounding a number upwards to the nearest integer, refer to the ceil() method.
Tip: To round a number to the nearest integer in either direction, consider using the round() method.
Note: For positive numbers the floor() method just removes the decimal part, but for negative numbers the integer part of the number will be changed if the number has a decimal part. If you only want to remove the decimal part, you can type cast the number as an integer. |
public static double floor(double number)
|
Parameter |
Description |
number |
Needed: A number to be rounded downwards. |
Returns: |
A double value representing the largest integer less than or equal to a given number. |
Java Version: |
Any |