Math.abs() / max() / min() / round() / floor() / ceil()
A utility class for mathematical operations, including getting the absolute value, maximum, and minimum of numbers; rounding, flooring, and ceiling; exponentiation, square roots, and random number generation. All methods are static, so you can call them as Math.methodName() without creating an instance.
Syntax
// Returns the absolute value. Math.abs(value); // Returns the larger of two values. Math.max(value1, value2); // Returns the smaller of two values. Math.min(value1, value2); // Rounds to the nearest integer (returns a long). Math.round(value); // Rounds down (returns a double). Math.floor(value); // Rounds up (returns a double). Math.ceil(value); // Calculates the power. Math.pow(base, exponent); // Returns the square root. Math.sqrt(value); // Returns a random number from 0.0 (inclusive) to 1.0 (exclusive). Math.random();
Method List
| Method | Description |
|---|---|
| Math.abs(x) | Returns the absolute value of the argument. Supports int, long, float, and double types. |
| Math.max(a, b) | Returns the larger of the two arguments. |
| Math.min(a, b) | Returns the smaller of the two arguments. |
| Math.round(x) | Returns the rounded value. Returns int for float input, and long for double input. |
| Math.floor(x) | Returns the largest integer less than or equal to the argument as a double (rounds down). |
| Math.ceil(x) | Returns the smallest integer greater than or equal to the argument as a double (rounds up). |
| Math.pow(a, b) | Returns a raised to the power of b as a double. |
| Math.sqrt(x) | Returns the square root of the argument as a double. |
| Math.random() | Returns a random double value from 0.0 (inclusive) to 1.0 (exclusive). |
Sample Code
// Get the absolute value. System.out.println(Math.abs(-5)); // Outputs: 5 System.out.println(Math.abs(3.14)); // Outputs: 3.14 // Get the maximum and minimum values. System.out.println(Math.max(10, 20)); // Outputs: 20 System.out.println(Math.min(10, 20)); // Outputs: 10 // Round, floor, and ceil. System.out.println(Math.round(3.5)); // Outputs: 4 System.out.println(Math.round(3.4)); // Outputs: 3 System.out.println(Math.floor(3.9)); // Outputs: 3.0 System.out.println(Math.ceil(3.1)); // Outputs: 4.0 // Calculate power and square root. System.out.println(Math.pow(2, 10)); // Outputs: 1024.0 System.out.println(Math.sqrt(16)); // Outputs: 4.0 // Generate a random integer from 1 to 10. int rand = (int)(Math.random() * 10) + 1; System.out.println(rand); // Outputs one of the values from 1 to 10.
Notes
Math.floor() and Math.ceil() return a double. Cast to (int) if you need an integer result (e.g., (int)Math.floor(3.9) returns 3).
Be careful with negative numbers when using Math.round(). Math.round(-3.5) returns -3 (rounds toward positive infinity). For more flexible random number generation, consider using java.util.Random or ThreadLocalRandom instead of Math.random().
For integer constants and base conversion, see 'Integer.MAX_VALUE / MIN_VALUE / toBinaryString()'.
If you find any errors or copyright issues, please contact us.