Ad space (banner)
Java Lessons
Lesson 45 / 68

Rounding Numbers (floor, ceil, round)

This lesson covers rounding decimals in Java — floor, ceil, and round — so you can pick the right rounding method for the job. It's for anyone searching "Java Math.round usage."

Math.floor() rounds down, Math.ceil() rounds up, and Math.round() rounds to the nearest whole number. It helps to picture "erasing down to the smaller number," "bumping up," and "rounding to whichever is closer."

The example shows Math.floor(3.7) giving 3.0, Math.ceil(3.2) giving 4.0, and Math.round(3.5) giving 4 — similar decimal values, but each method produces a different result.

A common early mistake is not noticing the difference in each method's return type. Math.floor() and Math.ceil() both return a double, while Math.round() returns a long (or int) — a distinction worth watching for.

In real projects, which rounding method you choose changes the result — rounding a monetary calculation's fraction, or aligning decimal places for display — so choose carefully depending on the situation.

📖 Reference code
✍️ Your code
Type your code, then press "Run"

🧪 This site can't compile or run Java directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser — nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)