Android get calculation result with decimal

Viewed 35

I'm making some calculations on my project in order to see how many trucks could be parked on a road of X meters.

I have this:

    int road1 = 140;
    double trucks = 0;
    
    trucks = (road1-10)/20;
System.out.println("trucks"+trucks);

The problem is that if you calculate by yourself, the result will be 6.5. So you could park 6.5 trucks.

But I always get 6.0

1 Answers

You are doing the calculation with ints, if you change to trucks = (road1-10)/20.0 you will get the answer 6.5

Related