user1420042
user1420042

Reputation: 1709

Why do I get a wrong result for this calculation in my android app?

In my app i am using this to calculate a number:

double result = ((a * a) + (b * b) - (2 * a * b));

If a for example a = 2 and b = 3.9, then the result should be 3.61. However, when I display "result" i get the number 3.610000000000001.

What is wrong here? How can I correct this error?

Upvotes: 0

Views: 258

Answers (1)

Pavel Dudka
Pavel Dudka

Reputation: 20944

You can format your output value by using the following construct:

String result;
twoDForm = new DecimalFormat("#0.00");
result = twoDForm.format(value);

After this, in result you will have your value in X.XX format

Upvotes: 3

Related Questions