The example code that can be used to format minor currency formatting, e.g. 10011 USD is 100.11 USD after formatting. The decimal place is base on the currency of the amount.
import java.text.NumberFormat
import java.util.Locale
import java.util.Currency
import java.math.BigDecimal
import java.math.BigInteger
String currency = "USD";
String amount = "10011";
for(Locale locale : NumberFormat.getAvailableLocales()){
String code = NumberFormat.getCurrencyInstance(locale).getCurrency().getCurrencyCode();
if ( currency.equals(code.trim())) {
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
Currency ccy = Currency.getInstance(locale);
numberFormat.setCurrency(ccy);
BigDecimal formattedAmount = new BigDecimal(new BigInteger(amount),numberFormat.getMaximumFractionDigits());
System.out.println(formattedAmount);
}
}
No comments:
Post a Comment