Search in sources :

Example 1 with CurrencyUnit

use of org.joda.money.CurrencyUnit in project killbill by killbill.

the class DefaultInvoiceFormatter method getFormattedAmountByLocaleAndInvoiceCurrency.

// Returns the formatted amount with the correct currency symbol that is get from the invoice currency.
private String getFormattedAmountByLocaleAndInvoiceCurrency(final BigDecimal amount) {
    final String invoiceCurrencyCode = invoice.getCurrency().toString();
    final CurrencyUnit currencyUnit = CurrencyUnit.of(invoiceCurrencyCode);
    final DecimalFormat numberFormatter = (DecimalFormat) DecimalFormat.getCurrencyInstance(locale);
    final DecimalFormatSymbols dfs = numberFormatter.getDecimalFormatSymbols();
    dfs.setInternationalCurrencySymbol(currencyUnit.getCode());
    try {
        Currency currency = Currency.fromCode(invoiceCurrencyCode);
        dfs.setCurrencySymbol(currency.getSymbol());
    } catch (final IllegalArgumentException e) {
        dfs.setCurrencySymbol(currencyUnit.getSymbol(locale));
    }
    numberFormatter.setDecimalFormatSymbols(dfs);
    numberFormatter.setMinimumFractionDigits(currencyUnit.getDecimalPlaces());
    numberFormatter.setMaximumFractionDigits(currencyUnit.getDecimalPlaces());
    return numberFormatter.format(amount.doubleValue());
}
Also used : CurrencyUnit(org.joda.money.CurrencyUnit) DecimalFormatSymbols(java.text.DecimalFormatSymbols) DecimalFormat(java.text.DecimalFormat) Currency(org.killbill.billing.catalog.api.Currency)

Aggregations

DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 CurrencyUnit (org.joda.money.CurrencyUnit)1 Currency (org.killbill.billing.catalog.api.Currency)1