我有一个下游服务(Stripe),它要求我以最小的货币单位发送货币(在他们的文档中是0-decimal货币)。也就是说,如果收费$1,我会发送{ "currency":"USD",金额: 100 },如果收费100元,我会发送{ "currency":"YEN",金额:100}
我的上游应用程序不希望以这种方式处理货币,而希望使用标准货币格式。有没有办法把javax.money.MonetaryAmount转换成零十进制的货币格式?
或者我必须手动编写转换?
发布于 2021-11-18 17:28:39
我见过一些人使用BigDecimal。这里它是一个函数。请为它编写一些测试:):
public static BigDecimal currencyNoDecimalToDecimal(int amount, String currencyCode) {
Currency currency = Currency.getInstance(currencyCode); // ISO 4217 codes
BigDecimal bigD = BigDecimal.valueOf(amount);
System.out.println("bigD = " + bigD); // bigD = 100
BigDecimal smallD = bigD.movePointLeft(currency.getDefaultFractionDigits());
System.out.println("smallD = " + smallD); // smallD = 1.00
return smallD;
}
public static void main(String[] args) {
int amount = 100;
String currencyCode = "USD";
BigDecimal dollars = currencyNoDecimalToDecimal(amount, currencyCode);
System.out.println("dollars = "+dollars);
}https://stackoverflow.com/questions/44674280
复制相似问题