Mahmut K.
Mahmut K.

Reputation: 850

Why the results of decimal operation is incorrect?

I want to use as a double value to 2 decimal places. But I think I got Incorrect value.

1.965 = must be 1.97 but result is 1.96

I tired them:

DecimalFormat("0.00") or DecimalFormat("#.##") or NumberFormat.getInstance() / ..setMaximumFractionDigits(2)

but the result has not changed.

Thanks..

    Log.e("Result:", String.format("%.2f", 1.965 )); // 1.96  < ?
    Log.e("Result:", String.format("%.2f", 1.975 )); // 1.98
    Log.e("Result:", String.format("%.2f", 1.955 )); // 1.96
    Log.e("Result:", String.format("%.2f", 1.9650 )); // 1.96 < ?
    Log.e("Result:", String.format("%.2f", 1.9651 )); // 1.97
    Log.e("Result:", String.format("%.2f", 1.966 )); // 1.97

Upvotes: 0

Views: 70

Answers (1)

Hellmar Becker
Hellmar Becker

Reputation: 2982

The reason is in the way that floating point numbers are repesented internally. They are of the form (binary mantissa) * 2^(binary exponent). Usually, any decimal number cannot be represented exactly in binary form, so some rounding takes place already when calculating that representation.

In a nutshell, your number 1.965 is likely to end up represented as a binary floating point number that is a tiny bit less than 1.965, and when your program rounds it to 2 decimal places, you get this kind of unexpected result.

Here http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html is a comprehensive overview of the pitfalls of floating point arithmetic.

Upvotes: 1

Related Questions