0

I want to format a BigDecimal as a currency in one Locale but use the currency symbol of another. In this particular case it's a Swedish user but the amount is in Euro.

My hope is that you can show me a better way than this:

public String getFormattedAmount()
{
    Locale.setDefault(new Locale(currentUser.getLocale()));
    NumberFormat nf = NumberFormat.getCurrencyInstance();
    DecimalFormatSymbols decimalFormatSymbols = ((DecimalFormat) nf).getDecimalFormatSymbols();
    decimalFormatSymbols.setCurrencySymbol("");
    ((DecimalFormat) nf).setDecimalFormatSymbols(decimalFormatSymbols);
    return nf.format(amount).trim();
}

Play 2.2.

Jonas Elfström
  • 28,718
  • 6
  • 66
  • 102
  • Dear Close voters, The similar question has nothing to do with Play Framework. Play's former template language had `${ 42.formatCurrency('EUR').raw() }` but I can't seem to find anything similar in the current one. I figured it might still be there even though I can't find it. – Jonas Elfström Nov 05 '14 at 14:18

1 Answers1

0

You can try this solution (inspired in this answer):

DecimalFormat nf = (DecimalFormat) DecimalFormat.getNumberInstance(new Locale(currentUser.getLocale()));
nf.setMinimumFractionDigits(2);
nf.setMaximumFractionDigits(2);
String formattedValue = nf.format(amout);
Community
  • 1
  • 1
cassiomolin
  • 101,346
  • 24
  • 214
  • 283