2017-01-17 57 views
1

我只是碰到JScience的這種古怪的行爲來(4.3.1)與虛構的轉換率1.05轉換到歐元時,美元(意思是,我得到1.05美元,如果我付1歐元):JScience貨幣單位轉換是否倒置?

Currency unitMoney1 = Currency.EUR; 
Currency unitMoney2 = Currency.USD; 
Currency.setReferenceCurrency(unitMoney1); 
unitMoney2.setExchangeRate(1.05); 
result = unitMoney1.getConverterTo(unitMoney2).convert(1.0); 
System.out.println(result); 
    //prints 0.9523809523809523 (unexpected, should be 1.05) 
result = unitMoney2.getConverterTo(unitMoney1).convert(result); 
System.out.println(result); 
    //prints 1.0 (expected) 

轉換,從一個長度單位到另一個作品是不同的:

Unit<Length> unitLength1 = (Unit<Length>) Unit.valueOf("m"); 
Unit<Length> unitLength2 = (Unit<Length>) Unit.valueOf("mm"); 
double result = unitLength1.getConverterTo(unitLength2).convert(1.0); 
System.out.println(result); 
// prints 1000.0 

也許我只是有一個結在我的大腦,但即使在這個最小再現我似乎沒有想出解決辦法。

回答

1

setExchangeRate()方法「設置此Currency相對於參考貨幣的匯率。」在您的示例中,參考貨幣應爲Currency.USD,而不是Currency.EUR

Currency.setReferenceCurrency(Currency.USD); 
Currency.EUR.setExchangeRate(1.05); // 1.0 € = 1.05 $ 
System.out.println(Currency.EUR.getConverterTo(Currency.USD).convert(1.0)); 

按預期打印1.05。另見setReferenceCurrency()