Why double width = 50/110000; the output is 0.000000000000000?

Because you’re dividing two integers, so it will only take the integer part (integer division).

Dividing integers in a computer program requires special care. Some
programming languages, treat integer division (i.e by giving the integer quotient as the answer). So the answer is an integer.

Examples :

In real life                  In Java

4/3  = 1.33333                4/3  = 1
25/12 = 2.083333              25/12 = 2
9/2 = 4.5                     9/2 = 4
50/110000 = 0.000454545       50/110000 = 0

You can cast one of the number (or both but it’s actually useless) to double to avoid that :

double width = (double)50/110000;
double width = 50d/110000;
double width = 50.0/110000;

Leave a Comment