GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — Rounding off errors in Java - GeeksforGeeks Rounding off errors in JavaLast 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.
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 ErrorJava'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.
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:
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 How to Rectify Round Off Errors?
Output a = 0.1 Explanation Important Note About Math.round()
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 Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |