# float division

``````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?
LVL 7
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Database ExpertCommented:
You are getting value in the integer fields. You need to use Float/Decimal types.
Please note integer data type will store only integer values.

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

Output
0.1
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Director of OperationsCommented:
Pawan is correct, but as a side sometimes the variable you need to work comes to you as an int.  You can simply cast the type as such:

``````int y = 10;
float x = (float)y / 100;
``````
0
Database ExpertCommented:
Yes you can do this also if you want to preserve this (int y) and use it somewhere else.
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
0
Commented:
you could do

``````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
0
Commented:
The first answer was the solution. The other comments may help to understand the difference of a float division compared to an integer division.

Sara
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.