Gotchas with numbers in Java
Using numbers in Java can be tricky if you don't know what shadowing is doing with them.
When using numbers in Java, if you don’t explicit tell the compiler what type of number it expects it treats most numbers as integers. This becomes a problem with rounding and doing math on numbers to get the right accuracy in the calculations.
For instance, if you are rounding a number:
Math.round(1525443794);
The above is not equal to the below when its explicit with a d
:
Math.round(1525443794d)
The reason this is different is because its trying to round an int
that is already rounded. Its related to when Java converted from 32 to 64 bits and how the number management is done now. The d
works with an explicit int, but you’ll need to cast the number if its a variable.
For instance:
Number number = 1525443794;
Math.round((double)number);
The issue with Math.round
is the initial casting is to a float
if an int is provided. WHen it does this, it loses the precision once the number is converted back to an int. The number ends up being different. Using a double will help you avoid some issues that floats introduce. Unless of course you deal with floats a lot, go for it.
A lot of times in financial systems the number is converted to a long with the 2
decimal points being converted up through multiplying it by 100. For display and some reporting the number is then converted back to the decimal form through dividing by 100.
Hope this helps, so watch out for precision with how the Math
library converts numbers internally to do its operations.