Backend Development 8 min read

Understanding Java BigDecimal: Solving Floating‑Point Precision Problems

This article explains why floating‑point numbers lose precision in binary representation, demonstrates the issue with Java float examples, and shows how to use the BigDecimal class—including creation, arithmetic operations, scaling, and proper equality comparison—to achieve exact decimal calculations in Java applications.

IT Services Circle
IT Services Circle
IT Services Circle
Understanding Java BigDecimal: Solving Floating‑Point Precision Problems

BigDecimal is a frequently asked interview topic in large‑scale Java development because it can avoid the precision loss that occurs when using primitive floating‑point types.

Why Float and Double Lose Precision

Computers store numbers in binary with a limited width; infinite‑length decimal fractions must be truncated, causing loss of accuracy. For example, the decimal 0.2 cannot be represented exactly in binary, leading to rounding errors.

float a = 2.0f - 1.9f;
float b = 1.8f - 1.7f;
System.out.println(a); // 0.100000024
System.out.println(b); // 0.099999905
System.out.println(a == b); // false

The above output shows that two mathematically equal expressions produce different results due to binary representation limits.

Introducing BigDecimal

BigDecimal performs arithmetic on decimal numbers without precision loss, making it suitable for scenarios such as monetary calculations.

BigDecimal a = new BigDecimal("1.0");
BigDecimal b = new BigDecimal("0.9");
BigDecimal c = new BigDecimal("0.8");
BigDecimal x = a.subtract(b);
BigDecimal y = b.subtract(c);
System.out.println(x.compareTo(y)); // 0

Creating BigDecimal Instances

To prevent precision loss, prefer the BigDecimal(String val) constructor or BigDecimal.valueOf(double val) static method.

Arithmetic Operations

The add , subtract , multiply , and divide methods operate on two BigDecimal objects. When dividing, use the three‑argument version to specify scale and RoundingMode to avoid ArithmeticException .

BigDecimal a = new BigDecimal("1.0");
BigDecimal b = new BigDecimal("0.9");
System.out.println(a.add(b));        // 1.9
System.out.println(a.subtract(b));   // 0.1
System.out.println(a.multiply(b));   // 0.90
System.out.println(a.divide(b, 2, RoundingMode.HALF_UP)); // 1.11

Scaling and Rounding

Use setScale(int scale, RoundingMode roundingMode) to define the number of decimal places and the rounding rule.

BigDecimal m = new BigDecimal("1.255433");
BigDecimal n = m.setScale(3, RoundingMode.HALF_DOWN);
System.out.println(n); // 1.255

Equality Comparison

Calling equals() on BigDecimal compares both value and scale, so new BigDecimal("1") and new BigDecimal("1.0") are not equal. Use compareTo() to compare only the numeric value.

BigDecimal a = new BigDecimal("1");
BigDecimal b = new BigDecimal("1.0");
System.out.println(a.equals(b));    // false
System.out.println(a.compareTo(b)); // 0

Summary

Floating‑point numbers cannot be represented exactly in binary, which leads to precision loss. Java’s BigDecimal class, backed by BigInteger , provides precise decimal arithmetic by handling both integer and fractional parts.

References

[1] Computer System Fundamentals (Part 4) – Floating‑Point Numbers: http://kaito-kidd.com/2018/08/08/computer-system-float-point/

JavaPrecisionArithmeticBigDecimalcomparisonFloating Point
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.