Currency Formatting Canadian English and French
Asked Answered
M

2

7

I have a requirement to set the value based on the locale. I will get the locale either en_US or fr_FR

 String locale = object.getLocale();  //

Then based on the locale I need to define the currency. The currency format I need is defined under.

   Language            Example             Notes
   Canadian French     123.456.789,99 $    symbol is a suffix,'comma' for decimal
   Canadian English    $123,456,789.99      symbol is a prefix,'dot' for decimal

Currently I have form attributes which I directly set the values in my java class.

  ...
  Form form = new Form();
    // Stub data for generating a graph.
    formBean.setCurrOne("123.54");
    formBean.setCurrTwo("456.33");
          ....//before I set those attributes I need to place a check
              // for locale and format the currency accordingly.

Can you please help me with the format. Also there is a difference of , and a . in the currency format.

Marylouisemaryly answered 26/12, 2011 at 10:45 Comment(0)
T
9
NumberFormat canadaFrench = NumberFormat.getCurrencyInstance(Locale.CANADA_FRENCH);
NumberFormat canadaEnglish = NumberFormat.getCurrencyInstance(Locale.CANADA);

BigDecimal amount = new BigDecimal("123456789.99");

System.out.println(canadaFrench.format(amount));
System.out.println(canadaEnglish.format(amount));

Result:

123 456 789,99 $
$123,456,789.99

If you really don't want to use the default format (with spaces as thousand separator rather than dots), then use

DecimalFormatSymbols symbols = ((DecimalFormat) canadaFrench).getDecimalFormatSymbols();
symbols.setGroupingSeparator('.');
((DecimalFormat) canadaFrench).setDecimalFormatSymbols(symbols);

See, this is all done for you by the NumberFormat class, provided you give it the proper locale. fr_FR means French of France, and not French of Canada. You need fr_CA for that. And en_US means English of the United States, and not English of Canada. You need en_CA for that.

Torsibility answered 26/12, 2011 at 11:36 Comment(1)
Can this be done using org.springframework.format.number.CurrencyFormatter ANy example?Marylouisemaryly
R
4

Joda Money provides a nice model for, well, money supporting specialized formatting for different currencies. Example code:

CurrencyUnit canadianFrench = CurrencyUnit.getInstance(Locale.CANADA_FRENCH);
CurrencyUnit canadianEnglish = CurrencyUnit.getInstance(Locale.CANADA);

MoneyFormatter canadianFrenchFormat = new MoneyFormatterBuilder().
        appendAmount(MoneyAmountStyle.ASCII_DECIMAL_COMMA_GROUP3_DOT).
        appendLiteral(" $").
        toFormatter();
MoneyFormatter canadianEnglishFormat = new MoneyFormatterBuilder().
        appendLiteral("$").
        appendAmount(MoneyAmountStyle.ASCII_DECIMAL_POINT_GROUP3_COMMA).
        toFormatter();

System.out.println(canadianFrenchFormat.print(Money.of(canadianFrench, 123456789.99)));
System.out.println(canadianEnglishFormat.print(Money.of(canadianEnglish, 123456789.99)));

A bit verbose, huh :)

Refined answered 27/12, 2011 at 12:41 Comment(3)
Can this be done using org.springframework.format.number.CurrencyFormatter ?? Any helpMarylouisemaryly
This answer is really helpful, one question i would like to ask is how can we add formatter for negative amount? I need (900.00,00) in case of -90000.00Dirtcheap
@Sanjaya Pandey There doesn't seem to be a way to do that with joda-money (v0.10). You can set the negativeCharacter in MoneyAmountStyle but that won't help in the kind of formatting you're looking for.Refined

© 2022 - 2024 — McMap. All rights reserved.