0Pricing
Java Academy · Lesson

Math Rounding and Limits

floor, ceil, round, min, max.

Math Rounding and Limits is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Rounding and Limits

This lesson focuses on the rounding family of Math methods (floor, ceil, round) and on clamping values using min and max.

Math.floor

Math.floor rounds down to the nearest integer value, returning a double. Math.floor(2.9) is 2.0.

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.floor(2.9));
        System.out.println(Math.floor(-2.1));
    }
}

Math.ceil

Math.ceil rounds up. Math.ceil(2.1) is 3.0, and Math.ceil(-2.9) is -2.0.

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.ceil(2.1));
        System.out.println(Math.ceil(-2.9));
    }
}

Math.round

Math.round rounds to the nearest integer, with ties (.5) rounding up. For a double argument it returns a long.

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.round(2.4));
        System.out.println(Math.round(2.5));
        System.out.println(Math.round(-2.5));
    }
}

Negative Rounding Surprises

For negatives, floor goes more negative and ceil goes toward zero. Math.round(-2.5) is -2 because it rounds half up (toward positive infinity).

Truncation by Casting

Casting a double to int simply drops the fractional part (truncates toward zero). This is different from floor for negatives.

public class Main {
    public static void main(String[] args) {
        System.out.println((int) 2.9);
        System.out.println((int) -2.9);
        System.out.println((int) Math.floor(-2.9));
    }
}

Min and Max as Limits

Combine Math.max and Math.min to clamp a value into a range. Math.max(0, Math.min(100, v)) keeps v between 0 and 100.

public class Main {
    public static void main(String[] args) {
        int v = 130;
        int clamped = Math.max(0, Math.min(100, v));
        System.out.println(clamped);
    }
}

Math.clamp

Modern Java added Math.clamp(value, min, max) which does the clamping directly and is easier to read than nested min/max calls.

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.clamp(130, 0, 100));
        System.out.println(Math.clamp(-5, 0, 100));
        System.out.println(Math.clamp(50, 0, 100));
    }
}

floorDiv and floorMod

Integer division truncates toward zero, which surprises with negatives. Math.floorDiv and Math.floorMod round toward negative infinity, giving consistent results.

public class Main {
    public static void main(String[] args) {
        System.out.println(-7 / 2);
        System.out.println(Math.floorDiv(-7, 2));
        System.out.println(Math.floorMod(-7, 2));
    }
}

Rounding to Decimal Places

To round to N decimals, scale up, round, then scale down: round x to 2 places with Math.round(x * 100.0) / 100.0.

public class Main {
    public static void main(String[] args) {
        double price = 3.14159;
        double rounded = Math.round(price * 100.0) / 100.0;
        System.out.println(rounded);
    }
}

Choosing a Rounding Method

Use floor to always round down, ceil to always round up, round for nearest, and a cast for truncation. For limits, prefer Math.clamp.

Quick Check

Test your understanding of rounding.

Recap

You learned rounding and limits.

  • floor rounds down, ceil rounds up, round rounds to nearest (ties up).
  • A cast truncates toward zero.
  • Math.clamp (or nested min/max) constrains a value to a range.
  • floorDiv and floorMod handle negatives consistently.

Frequently asked questions

Is the “Math Rounding and Limits” lesson free?

Yes — the full text of “Math Rounding and Limits” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Math Rounding and Limits”?

floor, ceil, round, min, max. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Math Rounding and Limits” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Math Class
  2. Generating Random Numbers
  3. Random Ranges and Shuffling
  4. Math Rounding and Limits
← Back to Java Academy