What is wrong???

Hello,

I have this code:
System.out.println(((((25 * 1) / 100)) + 1) * 64);

it’s returning 64 but it should return 80…

Why?

Tks, Joaogl


System.out.println((int) (((((25 * 1) / 100F)) + 1) * 64));

What is the purpose of that? I’m curious…

25 / 100

You think this number is .25, really it is 0.

That is because the computer sees 25 is an integer (int) and 100 is an integer and therefore, 25/100 must be an integer.

Obviously, this is not how math works, but this is how java thinks about it. However, if either 25 or 100 is a double (double precision number), then you will get the answer you want. You can just do this by writing 25.0 instead of 25 or 100.0 instead of 100.

I made a little list of code and what it should output.

System.out.println(25/100); —> 0
System.out.println(25/20); —> 1
System.out.println(25.0/100); —> 0.25

FIFY

Fair enough :stuck_out_tongue:

I thought I’d make sure the OP knows that there are floats too.

There isn’t anything wrong with the statement (apart from the computer part. Computers do what they’re told to do. They don’t care whether one number is an int or a double. The JVM does that, and the compiler tells the JVM that the result should be an int/double). It’s just that it’s important to note that there are floats too.