Java Math floor() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

In Java, Math.floor(a) returns the largest whole number that is less than or equal to a. It rounds down: Math.floor(4.9) is 4.0.

"Down" means toward negative infinity, so Math.floor(-2.5) is -3.0. That is the key difference from an (int) cast, which simply chops off the decimals and gives -2.

A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.floor(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass a double; the whole-number result is still a double.

Java
double a = Math.floor(4.9);                // 4.0
double b = Math.floor(-2.5);               // -3.0
int cast = (int) -2.5;                     // -2 (truncates toward zero)
int cell = (int) Math.floor(x / size);     // grid cell, also for negative x
CallWhat it does
Math.floor(7.99)Returns 7.0.
Math.floor(-7.01)Returns -8.0.
(int) Math.floor(seconds / 60)Whole minutes in a number of seconds.
Math.floor(x * 100) / 100Cut to two decimals (see the pitfall below).

For int and long division that rounds down, skip the doubles and use Math.floorDiv (Java 8+), which is exact for every value.

Library declarations
Java
// java.lang.Math
public static double floor(double a)

Parameters

ParameterDescription
aThe value to round down. Smaller number types are widened to double.

Return Value

Return ValueDescription
doubleThe largest whole-number double that is <= a. NaN, infinities, zeros (including -0.0), and whole values come back unchanged.

Compare with Similar Methods

Rounding down, and its neighbours:

MethodHow it differs
ceil(a)Rounds up (toward positive infinity).
(int) aTruncates toward zero: same as floor for positives, different for negatives.
floorDiv(x, y)Integer division rounded down, no doubles needed.
round(a)Nearest whole number as a long.

Java Examples

Example 1 — Whole Minutes and Full Cartons

Split seconds into whole minutes plus the rest, and count how many full egg cartons you can pack.

Java
public class MathFloorWhole {
    public static void main(String[] args) {
        double seconds = 125.0;
        int minutes = (int) Math.floor(seconds / 60);
        System.out.println(minutes + " min " + (seconds - minutes * 60) + " s");

        int eggs = 50;
        System.out.println("Full cartons: " + (int) Math.floor(eggs / 12.0));
    }
}

How It Works

  1. Round the quotient down. 125.0 / 60 is about 2.08, and floor keeps only the 2 complete minutes.
  2. The leftover. 125.0 minus 2 * 60 is 5.0 seconds; it prints with .0 because seconds is a double.
  3. Only full cartons. 50 / 12.0 is about 4.17, and a partly filled carton does not count, so the answer is 4.

Example 2 — Grid Cells: floor vs (int) Cast for Negative Numbers

Map an x coordinate to a 5-unit grid cell. The cast gives the wrong cell left of zero.

Java
public class MathFloorGrid {
    public static void main(String[] args) {
        double[] xs = {7.5, -0.5, -7.5};
        double cellSize = 5.0;
        for (double x : xs) {
            int cell = (int) Math.floor(x / cellSize);
            int truncated = (int) (x / cellSize);
            System.out.println("x=" + x + " cell " + cell + " (cast gives " + truncated + ")");
        }
    }
}

How It Works

  1. Positive side agrees. 7.5 / 5.0 is 1.5; flooring and truncating both give cell 1.
  2. Negative side differs. -0.5 / 5.0 is -0.1. floor goes down to -1, the cell that really contains -0.5, while the cast chops toward zero and wrongly lands in cell 0.
  3. Consistent cells. With floor, every cell covers exactly 5 units ([-10, -5) is cell -2), so -7.5 is in cell -2, not -1.

Example 3 — Cutting to Two Decimals, and a Binary Surprise

Truncate prices to cents with floor(x * 100) / 100 and see where it breaks.

Java
public class MathFloorDecimals {
    public static void main(String[] args) {
        double price = 19.987;
        System.out.println("Cut to cents: " + Math.floor(price * 100) / 100);

        double tricky = 1.15;
        System.out.println("tricky * 100 = " + tricky * 100);
        System.out.println("Cut to cents: " + Math.floor(tricky * 100) / 100);
        System.out.println("floor(-0.0) = " + Math.floor(-0.0));
    }
}

How It Works

  1. Shift, floor, shift back. 19.987 * 100 is about 1998.7, floor gives 1998.0, and dividing by 100 prints 19.98.
  2. Why 1.15 fails. The double nearest to 1.15 is a hair below it, so multiplying by 100 gives 114.99999999999999, and floor drops it to 114, printing 1.14 instead of 1.15.
  3. Zero keeps its sign. -0.0 is already a whole number, so floor returns it unchanged, minus sign included.

Frequently Asked Questions

It rounds a number down to the nearest whole number and returns it as a double. Math.floor(4.9) returns 4.0.
For positive numbers they agree. For negative numbers, floor rounds down (Math.floor(-2.5) is -3.0) while (int) -2.5 truncates toward zero and gives -2.
So that it can handle every double, including values far larger than any int or long. Cast the result when you know it fits.
Because 1.15 cannot be stored exactly in binary. The product is 114.99999999999999, which rounds down to 114. Use BigDecimal or whole cents for money.
Use Math.floorDiv(a, b). Plain a / b rounds toward zero, which differs for negative results.

Next: floorDiv()

Integer division that rounds the quotient down, toward negative infinity.

floorDiv() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful