0

If I divide a double type variable, the Decimal part becomes zero.

a=13122/10;
System.out.println (a);

Prints

1312.0

As you can see, the Decimal part became zero when I divided it. But I need the value

1312.2

Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75
Asrar
  • 9
  • 2
  • "If I divide a double type variable" - neither `13122` nor `10` are double, those are ints. – Amongalen Dec 17 '20 at 14:51
  • Or perhaps: https://stackoverflow.com/questions/3144610/integer-division-how-do-you-produce-a-double – Hulk Dec 17 '20 at 14:53

2 Answers2

0

Your problem is that while you may have stored "a" as a double, you are really dividing two "ints" and saving that. When you divide 2 ints, the number automatically gets rounded down. So, it's rounded down to 1312.0.

What you need is this,

a = (double)13122/10;

or this:

a = 13122.0/10;
SatvikVejendla
  • 424
  • 5
  • 14
0

You are dividing integers. You can cast the division and then you are ok

double a = (double) 13122/10;
    System.out.println (a);
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
AirlineDog
  • 520
  • 8
  • 21