金額操作使用BigDecimal,使用double的話會有精度損失
MultiCurrencyMoney:
/**
* 構造器。
*
* <p>
* 創建一個具有金額<code>amount</code>元和指定幣種<code>currency</code>的貨幣對象。
*
* @param amount 金額,以元爲單位。
* @param currency 幣種。
*/
public MultiCurrencyMoney(String amount, Currency currency) {
this(new BigDecimal(amount), currency);
}
/**
* 構造器。
*
* <p>
* 創建一個具有金額<code>amount</code>和指定幣種的貨幣對象。
* 如果金額不能轉換爲整數分,則使用四捨五入方式取整。
*
* <p>
* 注意:由於double類型運算中存在誤差,使用四捨五入方式取整的
* 結果並不確定,因此,應儘量避免使用double類型創建貨幣類型。
* 例:
* <code>
* assertEquals(999, Math.round(9.995 * 100));
* assertEquals(1000, Math.round(999.5));
* money = new Money((9.995));
* assertEquals(999, money.getCent());
* money = new Money(10.005);
* assertEquals(1001, money.getCent());
* </code>
*
* @param amount 金額,以元爲單位。
* @param currency 幣種。
*/
public MultiCurrencyMoney(double amount, Currency currency) {
this.setCurrency(currency);
// this.cent = Math.round(amount * getCentFactor());
this.cent = Math.round(CheckOverflow.doubleCheckedMultiply(amount, getCentFactor()));
}