[英]Is JScience money unit conversion inverted?
I just came across this odd behaviour of JScience (4.3.1) when converting EUR to USD with the fictive conversion rate 1.05
(meaning I get 1.05
USD if I pay 1 EUR):我刚刚在将欧元转换为美元时遇到了 JScience (4.3.1) 的这种奇怪行为,虚拟转换率为1.05
(这意味着如果我支付 1 欧元,我会得到1.05
美元):
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)
Conversion from one length unit into another works differently:从一种长度单位转换为另一种长度单位的工作方式不同:
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
Maybe I just have a knot in my brain, but even in this minimal reproduction I don't seem to figure this out.也许我的脑子里只有一个结,但即使在这个最小的复制中,我似乎也没有弄清楚这一点。
The setExchangeRate()
method "Sets the exchange rate of this Currency
relatively to the reference currency." setExchangeRate()
方法“设置此Currency
相对于参考货币的汇率。” In your example, the reference currency should be Currency.USD
, not Currency.EUR
.在您的示例中,参考货币应该是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));
This prints 1.05
, as expected.正如预期的那样,这会打印1.05
。 See also setReferenceCurrency()
.另请参阅setReferenceCurrency()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.