Is “long x = 1/2” equal to 1 or 0, and why? [duplicate]
This question already has an answer here: Integer division: How do you produce a double? 10 answers if I have something like: long x = 1/2; shouldn't this be rounded up to 1? When I print it on the screen it say 0. It's doing integer division, which truncates everything to the right of the decimal point. Integer division has its roots in number theory. When you do 1/2 you are asking how many times does 2 equal 1? The answer is never, so the equation becomes 0*2 + 1 = 1, where 0 is the quotient (what you get from 1/2) and 1 is the remainder (what you get from 1%2). It is right to point out that