我必须将我的Web应用程序与支付网关集成.我想以美元输入总金额,然后将其转换为美分,因为我的支付网关库接受的数量为Cents(类型Integer).我发现Big Decimal在java中操纵货币是最好的方法.目前我接受输入为50美元并将其转换为Integer如下:
BigDecimal rounded = amount.setScale(2, BigDecimal.ROUND_CEILING);
BigDecimal bigDecimalInCents = rounded.multiply(new BigDecimal("100.00"));
Integer amountInCents = bigDecimalInCents.intValue();
Run Code Online (Sandbox Code Playgroud)
这是将美元转换为美分的正确方法还是应该以其他方式实现?