Currency Format Canadian English and French

I have a requirement to set a 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 determine the currency. The currency format I need is defined below.

  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 that I directly set 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. 

Could you help me in the format. There is also a difference , and . in currency format.

+4
source share
2 answers
 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 a thousands separator, not dots), use

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

See, this is all done for you by the NumberFormat class if you give it the correct locale. fr_FR means French, not French. For this you need fr_CA. And en_US means English in the United States, not English in Canada. For this you need en_CA.

+5
source

Joda Money is a great model for money that supports specialized formatting for different currencies. Code example:

 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 detailed, huh :)

+3
source

Source: https://habr.com/ru/post/1388005/


All Articles