Rounding off errors in Java

Last Updated : 12 Jun, 2026

Rounding off errors occur in Java when floating-point numbers (float and double) cannot be represented exactly in binary format. Since computers store real numbers using a finite number of bits, some decimal values are approximated, which can lead to small inaccuracies in calculations.

  • Floating-point numbers follow the IEEE 754 standard.
  • Some decimal values, such as 0.1, cannot be represented exactly in binary.
  • Arithmetic operations on floating-point numbers may produce small precision errors.
Java
public class Main {
    public static void main(String[] args)
    {
        double a = 0.7;
        double b = 0.9;
        double x = a + 0.1;
        double y = b - 0.1;

        System.out.println("x = " + x);
        System.out.println("y = " + y );
        System.out.println(x == y);
    }
}

Output
x = 0.7999999999999999
y = 0.8
false

Explanation: The value 0.1 cannot be represented exactly in binary format. Therefore, x and y contain slightly different values internally, causing the comparison x == y to return false.

Reason Behind Round Off Error

Java's float and double data types follow the IEEE 754 floating-point standard, where numbers are stored in binary form as Sign × Fraction × 2Exponent Since many decimal values cannot be represented exactly in binary, Java stores the closest possible approximation, which can lead to small rounding errors during calculations.

  • Numbers are stored in binary rather than decimal.
  • Some decimal fractions cannot be represented exactly in binary.
Java
public class Main {
    public static void main(String[] args) {
        double a = 0.1;
        double b = 0.2;

        System.out.println(a + b);
    }
}

Output
0.30000000000000004

Explanation: The values 0.1 and 0.2 cannot be represented exactly in binary format. Therefore, Java stores approximate values, and their sum results in 0.30000000000000004 instead of exactly 0.3

Example:

Java
public class Main {
    public static void main(String args[])
    {
        double a = 1.0;
        double b = 0.10;
        double x = 9 * b;
        a = a - (x);

        // Value of a is expected as 0.1
        System.out.println("a = " + a);
    }
}

Output
a = 0.09999999999999998

Explanation: The value 0.10 cannot be represented exactly in binary, so Java stores an approximate value. When the multiplication and subtraction operations are performed, a small floating-point precision error occurs, resulting in the output 0.09999999999999998 instead of the expected 0.1.

How to Rectify Round Off Errors?

  • Round the result: The Round() function can be used to minimize any effects of floating point arithmetic storage inaccuracy. The user can round numbers to the number of decimal places that is required by the calculation. For example, while working with currency, you would likely round to 2 decimal places.
  • Algorithms and Functions: Use numerically stable algorithms or design your own functions to handle such cases. You can truncate/round digits of which you are not sure they are correct (you can calculate numeric precision of operations too)
  • BigDecimal Class: You may use the java.math.BigDecimal class, which is designed to give us accuracy especially in case of big fractional numbers. The following program shows how the error can be removed:
Java
import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {
    public static void main(String args[]) {
        BigDecimal a = new BigDecimal("1.0");
        BigDecimal b = new BigDecimal("0.10");
        BigDecimal x = b.multiply(new BigDecimal("9"));
        a = a.subtract(x);

        // Rounding to 1 decimal place
        a = a.setScale(1, RoundingMode.HALF_UP);

        System.out.println("a = " + a);
    }
}

Output
a = 0.1

Explanation: BigDecimal stores decimal values exactly instead of approximating them. The setScale() method rounds the result to one decimal place, producing the accurate value 0.1.

Important Note About Math.round()

  • Math.round() returns the nearest integer value.
  • It is not designed for high-precision decimal calculations.
  • Results may differ from BigDecimal.
Java
public class Main {
    public static void main(String args[])
    {
        double a = 1.0;
        double b = 0.10;
        double x = 9 * b;
        a = a - (x);

    /* We use Math.round() function to round the answer to
         closest long, then we multiply and divide by 1.0 to
         to set the decimal places to 1 place (this can be done
         according to the requirements.*/
        System.out.println("a = " + Math.round(a*1.0)/1.0);
    }
}

Output
a = 0.0

Explanation: The program performs the calculation 1.0 - (9 × 0.10), which produces a value very close to 0.1. The Math.round() method then rounds this value to the nearest whole number (0), and dividing by 1.0 converts it back to a double value

Comment