Math & Numbers
The Math class provides the functions you need for calculations — powers, roots, rounding, minimums and maximums, random numbers — and BigDecimal handles exact decimal arithmetic for money. This lesson covers both, plus how to generate random values properly.
Common Math methods
void main() { System.out.println(Math.max(4, 9)); System.out.println(Math.min(-2, 3)); System.out.println(Math.abs(-7.5)); System.out.println(Math.pow(2, 10)); System.out.println(Math.sqrt(144)); System.out.println(Math.cbrt(27)); System.out.println(Math.hypot(3, 4)); }
9 -2 7.5 1024.0 12.0 3.0 5.0
Math methods are static — you call them on the class itself. Note that pow and sqrt always return a double.
Rounding
void main() { double x = 7.45; System.out.println(Math.round(x)); System.out.println(Math.floor(x)); System.out.println(Math.ceil(x)); System.out.println(Math.round(x * 10) / 10.0); System.out.println("%.1f".formatted(x)); }
7 7.0 8.0 7.5 7.5
To round to decimal places for display, prefer formatting (%.1f). For calculations that must round exactly, use BigDecimal.
Constants and more functions
void main() { double r = 2.0; System.out.println(Math.PI * r * r); System.out.println(Math.E); System.out.println(Math.floorMod(-7, 3)); System.out.println(-7 % 3); System.out.println(Math.clamp(150, 0, 100)); }
12.566370614359172 2.718281828459045 2 -1 100
% keeps the sign of the left operand; Math.floorMod always returns a non-negative result for a positive divisor — what you want for wrapping indexes. Math.clamp (Java 21+) keeps a value within a range. There are also trigonometric (sin, cos…), logarithmic (log, log10) and exact-arithmetic (addExact, multiplyExact) methods.
Random numbers
void main() { var random = new java.util.Random(42); System.out.println(random.nextInt(1, 7)); System.out.println(random.nextInt(1, 7)); System.out.println(random.nextDouble() < 1.0); }
3 4 true
nextInt(origin, bound) returns a number from origin up to but excluding bound — here a dice roll from 1 to 6. Passing a seed (42) makes the sequence repeatable, which is useful for tests; leave it out for different numbers each run. Math.random() returns a double in [0, 1). For passwords and tokens, use java.security.SecureRandom.
BigDecimal for money
import java.math.BigDecimal; import java.math.RoundingMode; void main() { System.out.println(0.1 + 0.2); BigDecimal a = new BigDecimal("0.1"); BigDecimal b = new BigDecimal("0.2"); System.out.println(a.add(b)); BigDecimal price = new BigDecimal("19.99"); BigDecimal withVat = price.multiply(new BigDecimal("1.23")).setScale(2, RoundingMode.HALF_UP); System.out.println(withVat); }
0.30000000000000004 0.3 24.59
BigDecimal stores decimal numbers exactly. Create values from strings (new BigDecimal("0.1")) — creating them from a double copies the binary rounding error. Arithmetic uses methods (add, subtract, multiply, divide), and setScale rounds to a number of decimal places with an explicit rounding rule.
BigInteger
import java.math.BigInteger; void main() { BigInteger factorial = BigInteger.ONE; for (int i = 2; i <= 30; i++) { factorial = factorial.multiply(BigInteger.valueOf(i)); } System.out.println(factorial); }
265252859812191058636308480000000
BigInteger handles whole numbers of any size — 30! overflows even a long.
Try it yourself
Calculate monthly loan repayments with the standard formula.
Monthly payment: 460.59 Total paid: 22108.12