Julio Rosario Julio Rosario - 4 months ago 6
Java Question

Why does Eclipse doesn't display a number less than zero?

Every time I run this test program it display,

0.0 instead of 0.5


Does anyone know how to fix this in Eclipse?

public class Test {

public static void main(String[]args){

double distance;

distance = 1/2;

System.out.println(distance);
}

}

Answer

Your division is Integer division. There is no fault of Eclipse. Try following:

distance = 1/2.0;

OR

distance = 1.0/2;

Output:0.5