-2

I write a code in Java

I have two numbers

For example

int alas  = 15;
int tinggi = 3;

When I divide those two numbers

int luas = (alas * tinggi) / 2;

I want to have 7.5 as a result, but why it keeps shown as 7.0

2 Answers2

2

Try this:

double luas = (alas * tinggi) / 2.0;

when you divide int by int you will have int result. 2.0 mean that you want to divide by double, so result will be double.

DontRelaX
  • 744
  • 4
  • 10
1

int there is no decimal places. you can use double or float.

double luas = (alas * tinggi) / 2.0;
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • 2
    the part at the end - 2.0 - is very important, if it was just 2, then the result would have a decimal place, but it would always be 0 – Vladimir Marton May 31 '17 at 14:50