```
int y=10;
float x=y/100;
System.out.println(x)
```

i expected above result 0.1 but getting 0.0 wonder why?```
int y=10;
int x=y/100;
System.out.println(x)
```

why above is 0?please advise

```
int y = 10;
float x = (float)y / 100;
```

But apart from that Another VERY GOOD OPTION is to float the hard coded value rather than the variable so that you do not have to change anything.

Note that One of the value Numerator or the denominator has to be float other wise you will get the integer division.

```
public static void main(String args[])
{
int y=10;
float x = y / (float)100;
System.out.println(x);
}
```

OUTPUT

0.1

```
int y = 10;
System.out.println(y/100.);
```

if you have an math expression where one of the operands is a float or double the result also turns to a double. but if you assign the result to an int, it automatically was rounded doen to the next integer.

Please note integer data type will store only integer values.

... and because 0.1 is not an integer x was rounded to the next lower integer which is 0.

generally you can "use" that as a feature if you need rounding:

```
// we try to get a floating point number with 2 decimal places
double d = 12.345678;
double d100 = (d * 100.); // 1234.5678
double d100p = d100+0.5; //1235.0678
int x = (int)d100p; //1235
double drounded = x/100.; // 12.35
// all the above can be done with one line as well
double d = 1234.5678;
d = ((int)(d*100. + 0.5))/100.; // d=12.35
```

note, the trick is adding 0.5 what will round up floating point numbers which have 5 or greater as first digit after the decimal point to the next integer. decimals which have 4,3,2,1, or 0 as first fractional digit will get a 9,8,7,6,5 when you add 0.5. so they still were be rounded down when casting the expression to an int.

Sara

Sara

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.

Please note integer data type will store only integer values.

Updated code for you-

Open in new window

Output

0.1