Alex Birmingham wrote:Why do the following two lines of code equate to 0, vs 5.7 or 5?
System.out.println((12 / 21) * 10);
- and -
int = (int) ((12 /21) * 10);
Please read about "BODMAS". I am sure you will find your answer yourself.
- (12 /21) -- return 0.57 which is converted to 0 because it is int calculation by default.
- Then 0 will be multiplied by 10 which equals to 0.
- Therefore the answer will be Zero (0)
Note:
- If you change your code to......
System.out.println((12.0 /21) * 10);
Then the result will be what you were expecting.........