Rounding
Description
round, ceil, and floor handle decimal values differently.
Explanation
Choose the method that matches the rule your program needs.
Example
long rounded = Math.round(87.6);← Chapter 3 — Strings, Escapes & Math
mixed
The Math class provides common calculations without creating an object. Its methods are static, so call them with Math followed by a dot.
Math can round values, find powers, calculate square roots, and choose larger or smaller values. It helps make formulas easy to read and test.
round, ceil, and floor handle decimal values differently.
Choose the method that matches the rule your program needs.
long rounded = Math.round(87.6);pow, sqrt, max, and min solve common tasks.
Math.pow returns a double, even for whole-number inputs.
double area = Math.pow(5, 2);Math.random returns a decimal from 0.0 up to but not including 1.0. For most modern random-number needs, prefer java.util.Random or ThreadLocalRandom.
public class MathDemo {
public static void main(String[] args) {
double average = 87.6;
System.out.println(Math.round(average));
System.out.println(Math.max(72, 91));
}
}