,我幾乎可以肯定,你被點編號如何浮動工作缺乏瞭解咬傷。你不能再用二進制表示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