我有int值40959999
。如何将其转换为浮点而不四舍五入为409599,99
?
float f = 40959999/100.00f
的结果将是409600.0
答案 0 :(得分:0)
使用double而不是float
double d = 40959999/100.00f;
答案 1 :(得分:0)
Double
和Float
具有内在的不准确性,这是无法避免的(有关为什么here的更多信息)。在这种情况下,使用双精度数字可能无法四舍五入,但并非全部。如果您使用的东西永远都不能四舍五入(例如价格),那么应该改用BigDecimal
。
答案 2 :(得分:0)
按如下所示执行int
值。而不是使用float
,而是使用BigDecimal
。
BigInteger b1 = BigInteger.valueOf(40959999);
int scale = 2;
BigDecimal d1 = new BigDecimal(b1, scale);
System.out.println(d1);
run:
409599.99