What is the guaranteed accuracy of multiplication operator for double values in java?
For example, 2.2 * 100 is 220.00000000000003, but 220 is a double number. 220.00000000000003 is the next double after 220.
What is the guaranteed accuracy of multiplication operator for double values in java?
For example, 2.2 * 100 is 220.00000000000003, but 220 is a double number. 220.00000000000003 is the next double after 220.
you can use BigDecimal, but make sure that you use the construct of BigDecimal(String) rather than BigDecimal(double). The difference between them is:
The multiplication is working fine, but
2.2
cannot be represented exactly as a double. The closest doubles are:Some software will print the latter value as
2.2
, but that doesn't mean it's exact. It just means it's treated as "close enough".If you are working with financials data dont use
float or double
just usejava.math.BigDecimal