,我几乎可以肯定,你被点编号如何浮动工作缺乏了解咬伤。你不能再用二进制表示0.1,而用十进制表示1/3。然后,除此之外,IEEE浮点数的双精度数不能超过17位。
这不是Java或您的代码中的错误。
钱是一个不应该用十进制数表示的东西的典型例子。
写整数美元和美分Money类和使用,当你掌握足够的Java:
public class Money {
private final int dollars;
private final int cents;
private final Currency currency;
public Money(int dollars, int cents, Currency curr) {
if (dollars < 0) throw new IllegalArgumentException("dollars cannot be negative");
if (cents < 0) throw new IllegalArgumentException("cents cannot be negative");
if (curr == null) throw new IllegalArgumentException("currency cannot be null");
this.dollars = dollars;
this.cents = cents;
this.currency = curr;
}
// more methods here.
}
阅读float到int转换。简单的演员截断(向下舍入)。你想要一个“圆到最近”的转换。也就是说:'int remainingAmount =(int)((amount + 0.005)* 100);' – 2012-04-26 19:25:53