JScience的货币单位转换是倒置的吗?

时间:2017-01-17 00:54:52

标签: java jscience unit-conversion

我刚刚遇到了JScience(4.3.1)的这种奇怪行为,当我将虚拟转换率1.05转换为美元时(意味着如果我支付1欧元,我会获得1.05美元):< / p>

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 个答案:

答案 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()