0

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);
    }

}
E-Riz
  • 29,428
  • 8
  • 89
  • 125

1 Answers1

0

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

Kaushal28
  • 5,157
  • 4
  • 40
  • 65