> Java Help?

Java Help?

Posted at: 2014-12-18 
I have this line of code:

double x = (1 / (2 * 2));



System.out.println(x);

It keeps on returning 0 instead of what i want: 0.25. Any quick fixes?

Sure, this is super easy.

Anytime you do math with integers, it is considered "Integer math" and drops the decimals.

You can fix this by doing:

double x = (1.0 / (2.0 * 2.0));

You can look at hard casting of integers if you need to use this for variables, but I wanted to keep the answer as simple and straight forward as possible.

Good luck.

it deals with the expression as all integers

then sets it to X

it comes up with a decimal then sets it to 0 before x gets the decimal

so you cast double to the expression or add a decimal

double x = (double)1/(2+2);

OR

double x = 1.0/(2+2);

I have this line of code:

double x = (1 / (2 * 2));



System.out.println(x);

It keeps on returning 0 instead of what i want: 0.25. Any quick fixes?