Locale for Norwegian, Bokmal(Norway) issue

2019-05-30 19:44发布

问题:

I am facing problem while opening a downloaded CSV file in Excel under window 7 for above locale. Sometime the number is treated as date and all the cells are misaligned and formatted incorrectly.

I have written code for CSV download with the localized format. It's working perfectly for all the regions except this one.

I invested the issue and it came to know that there is a mismatch between format defined on Windows 7 system and locale retrieved from ServletRequest#getLocale() for Norwegian, Bokmal(Norway).

Here is my code:

Servlet:

Locale locale = request.getLocale();
DecimalFormat decimalFormatter=(DecimalFormat)DecimalFormat.getInstance(locale);

locale.getDisplayCountry();            // empty string
decimalFormatter.toLocalizedPattern(); // #,##0.###
locale.getDisplayLanguage();           // Norwegian Bokmål
locale.toString();                     // nb

Now look at the Digit grouping symbol and Decimal symbol in above code.


My Question: How can I get the correct number format pattern as it is on Windows 7?

Here is the screenshot of the Languages set in Firefox.

Here is the screenshot of Language and Region on Windows 7 where for numbers Digit grouping symbol is a single space and Decimal symbol is a comma.

回答1:

The language of the decimal format should work straight "out of the box".

    Locale locale = new Locale("nb");
    DecimalFormat decimalFormatter=(DecimalFormat) DecimalFormat.getInstance(locale);

    System.out.println(locale.getDisplayCountry());           
    System.out.println(decimalFormatter.toLocalizedPattern()); 
    System.out.println(decimalFormatter.toLocalizedPattern()); 
    System.out.println(locale.getDisplayLanguage());        

    System.out.println(decimalFormatter.format(1234.567));

Yields this result for me:

(empty)
# ##0,###
# ##0,###
Norwegian Bokmål
1 234,567

The decimal format is not controlled by the windows settings though, tried changing decimal separator to . in windows, with no effect.