Thomas Mahoney wrote:What's going on?
Integer division is going on.
Math.round() returns a long or int, depending on the type of the argument. The expression
1.2345 * 100 consists of a double and int, respectively. By promotion rules, the 100 is turned into a double, 100.0. So the entire expression is a double. That means you'll be calling
Math.round(double) and getting back a long, which you then divide by an int (100). By promotion rules, the result of that expression is a long. That long value gets promoted and assigned to the double variable.
If you just use a double value for the second 100, i.e., Math.round(1.2345 * 100) / 100.0 then you'll get the decimals you're expecting.
Note that in general, floating point operations are inherently imprecise and you won't always get exact decimals you expect if you use other numbers.