I have int value 40959999
. How to convert it to float without rounding into 409599,99
?
result of float f = 40959999/100.00f
will be 409600.0
I have int value 40959999
. How to convert it to float without rounding into 409599,99
?
result of float f = 40959999/100.00f
will be 409600.0
Double
and Float
have inherent imprecision that is impossible to avoid (more info on the why here). Using a double you may not have your number rounded in this scenario but not in all. If you work with something that should never be rounded (like prices), you should be using BigDecimal
instead.
Implement your int
value like shown. Rather than using float
use BigDecimal
instead.
BigInteger b1 = BigInteger.valueOf(40959999);
int scale = 2;
BigDecimal d1 = new BigDecimal(b1, scale);
System.out.println(d1);
run:
409599.99