【发布时间】:2015-08-04 13:24:37
【问题描述】:
我必须将我的 Web 应用程序与支付网关集成。我想以美元输入总金额,然后将其转换为美分,因为我的支付网关库接受以美分为单位的金额(Integer 类型)。我发现java中的Big Decimal是操纵货币的最佳方式。目前我输入 50 美元并将其转换为 Integer,如下所示:
BigDecimal rounded = amount.setScale(2, BigDecimal.ROUND_CEILING);
BigDecimal bigDecimalInCents = rounded.multiply(new BigDecimal("100.00"));
Integer amountInCents = bigDecimalInCents.intValue();
这是将美元转换为美分的正确方法还是我应该以其他方式进行?
【问题讨论】:
标签: java integer type-conversion bigdecimal