JAVA BASICS IN DETAIL

Java BigDecimal

BigDecimal is a class for precise decimal calculations. It is especially useful for money, interest and other values that must not be rounded inaccurately.

Why is double not always enough?

double stores numbers in binary. Some seemingly simple decimal fractions, such as 0.1, cannot be represented exactly in binary:

double pirmaKaina = 0.1;
double antraKaina = 0.2;

System.out.println(pirmaKaina + antraKaina);
Console output

0.30000000000000004

Such a tiny difference often does not matter for everyday measurements. In monetary calculations, however, repeated operations can accumulate a noticeable error.

Precise calculation with BigDecimal

import java.math.BigDecimal;

BigDecimal pirmaKaina = new BigDecimal("0.10");
BigDecimal antraKaina = new BigDecimal("0.20");
BigDecimal suma = pirmaKaina.add(antraKaina);

System.out.println(suma);
Console output

0.30

  1. The imported class is java.math.BigDecimal.
  2. Numbers are passed to the constructor as text, preserving their exact decimal values.
  3. Addition uses the method add()rather than the operator +.
  4. The result is stored in a new BigDecimal object.

How do you create BigDecimal safely?

BigDecimal isTeksto = new BigDecimal("19.99");
BigDecimal isSveikojo = BigDecimal.valueOf(12);
BigDecimal isDouble = BigDecimal.valueOf(0.1);

// Avoid: new BigDecimal(0.1)
  • new BigDecimal("19.99") suits an exact value provided as text.
  • BigDecimal.valueOf(12) is convenient for an integer.
  • BigDecimal.valueOf(0.1) uses the predictable double string representation.
  • new BigDecimal(0.1) preserves the existing binary double error, so it is best avoided.
Use text for money.

If you know the exact price in code, the clearest form is usually new BigDecimal("9.99").

Basic operations

Each operation below starts from the same initial kaina value, 12.50. Results are stored in separate variables, so one calculation does not become the starting point for another.

BigDecimal kaina = new BigDecimal("12.50");
BigDecimal kiekis = new BigDecimal("3");

BigDecimal suma = kaina.add(new BigDecimal("2.00")); // 12.50 + 2.00 = 14.50
BigDecimal skirtumas = kaina.subtract(new BigDecimal("1.50")); // 12.50 - 1.50 = 11.00
BigDecimal bendraKaina = kaina.multiply(kiekis); // 12.50 × 3 = 37.50

System.out.println(suma); // Prints 14.50
System.out.println(skirtumas); // Prints 11.00
System.out.println(bendraKaina); // Prints 37.50
Console output

14.50
11.00
37.50

BigDecimal arithmetic methods
OperationThe method
Additionadd()
Subtractionsubtract()
Multiplicationmultiply()
Divisiondivide()

BigDecimal objects are immutable

BigDecimal likutis = new BigDecimal("10.00");

likutis.add(new BigDecimal("5.00"));
System.out.println(likutis);

likutis = likutis.add(new BigDecimal("5.00"));
System.out.println(likutis);
Console output

10.00
15.00

The first add() creates a new result, but it is not saved. The original object stays unchanged. The second operation's result is assigned back to the variable, so we later see 15.00.

Division and rounding

Not every division has a terminating decimal result. When dividing 10 by 3, specify how many decimal places to keep and how to round:

import java.math.RoundingMode;

BigDecimal suma = new BigDecimal("10.00");
BigDecimal zmoniu = new BigDecimal("3");

BigDecimal dalis = suma.divide(zmoniu, 2, RoundingMode.HALF_UP);
System.out.println(dalis);
Console output

3.33

The number 2 specifies two decimal places. RoundingMode.HALF_UP means familiar rounding: five or a higher digit rounds up.

equals() and compareTo()

BigDecimal a = new BigDecimal("2.0");
BigDecimal b = new BigDecimal("2.00");

System.out.println(a.equals(b));
System.out.println(a.compareTo(b) == 0);
Console output

false
true

equals() checks both numeric value and scale, so 2.0 and 2.00 are different to it. compareTo() compares numeric values, so it considers the two numbers equal.

The cost of precision

BigDecimal value consists of an integer and a scale , the number of decimal places. For example, 12.50 can be understood as the integer value 1250 with a scale of 2.

This model offers high precision, but object creation and arithmetic methods generally use more memory and run slower than primitive double or long.

  • Choose BigDecimalwhen an exact decimal result matters, especially for money.
  • Choose doublewhen speed matters more and a small error is acceptable, as in many physics calculations.
  • You can use long for the smallest monetary units, such as cents, if the scale is clearly agreed across the system.
  • BigInteger is for very large integers without a fractional part.