struts2 format currency thousand separator

1.3k views Asked by At

I have a Struts 2 project where I display an item price. I want to change the format accorting to localization.

For example:

 locale en_GB : 75.9
 locale el_GR : 75,9

Currently I use the format in the messages for both locales :

{0,number,##0.00}

I tried changing the format in the one language to:

{0,number,##0,00}   

but then the displayed price was wrong as: 0,76 (moved everything to the decimal part).

Any suggestions?


EDIT:

Note that in my case i want to FORCE the comma as decimal separator for all locales in the application.

1

There are 1 answers

0
Andrea Ligios On

You can use the DecimalFormat Constructor accepting both a pattern and a DecimalFormatSymbols, created by its constructor taking a Locale:

Locale greekLocale = new Locale("el", "GR");

// Thousands separator, decimal separator etc...
DecimalFormatSymbols greekDFS = DecimalFormatSymbols.getInstance(greekLocale);

// Your pattern, their symbols
NumberFormat nf = new DecimalFormat("#,##0.0##", greekDFS);

Take a look at the running demo. I've used el_GR by the way, with gr_GR it seems that the decimal separator is still the dot. Not sure what gr_GR is.

P.S: Here is an answer related to formatting patterns and TextProvider in Struts2, that might help future visitors