0

I am trying to get exact calculated value (in my case it should be 66.66... not 66.0) but it prints 66.0

If I will get 66.66 then I can use Math.round(66.66) so that I will get 67

Below code after execution should return 66.66 but it returns 66.0

double d = (2*100)/3

Please suggest..

Regards

S Singh
  • 1,403
  • 9
  • 31
  • 47
  • 1
    possible duplicate of [Why does my java division code give the wrong answer?](http://stackoverflow.com/questions/7286681/why-does-my-java-division-code-give-the-wrong-answer) – talex Jul 01 '15 at 13:21

4 Answers4

3

(2*100)/3 performs integer multiplication and division, which results in an integer.

You need to force floating point calculation by changing one of the operands to an double (or float):

double d = (2.0*100)/3
Eran
  • 387,369
  • 54
  • 702
  • 768
1

As other answers suggested , you can provide at least one floating number. Or if you don't want to change the numbers, you can add cast to the numerator

double d=   (double)(2*100)/3;
System.out.println(d); 

prints

  66.66666666666667
Amit.rk3
  • 2,417
  • 2
  • 10
  • 16
  • You are not actually casting the RESULT to a `double`, rather, you are casting the NUMERATOR (e.g. `(2*100)`) to a `double` – user700390 Jul 01 '15 at 13:30
  • @user700390 : Yes. Thank you. Corrected it. – Amit.rk3 Jul 01 '15 at 13:33
  • I was trying (2*100/3) and was not using type casting (double). I think that was issue. double d= (double)(2*100)/3 returns expected result. – S Singh Jul 02 '15 at 09:11
0

You are using all int values in your arithmetic operation so final result will always be int and when you put it into a double then it becomes 66.0. But since original result is int, so you loose precision.

You can use double d = (2.0*100.0)/3.0; or have at least one value with decimal point, so that you can get expected decimal points.

Number after decimal point is commonly known as precision. So, the issue which you talked about is commonly known as floating-point imprecision, and in your case you can call it as double-point imprecision.

Rule of thumb:

  1. If either is of type double, the other is converted to double for arithmetic operation and final result will be a double.
  2. Otherwise, if either is a float, the other is converted to float for arithmetic operation and final result will be a float.
  3. Otherwise, if either is of type long, the other is converted to long for arithmetic operation and final result will be a long.
  4. Otherwise, both operands are converted to int for arithmetic operation and final result will be a int.
hagrawal7777
  • 14,103
  • 5
  • 40
  • 70
  • @SSingh: Have you got your answer, if not then please write your answer so that other's can be benefited from it. http://stackoverflow.com/help/accepted-answer – hagrawal7777 Jul 01 '15 at 20:29
0

As Eran says it's your expression perform integer arithmetic you need to perform like

double d = (double) (2*100)/3;

if needs format the result.

Shekhar Khairnar
  • 2,643
  • 3
  • 26
  • 44