Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DecimalFormatSymbols with Locale

For the code below I get a different result when I run it like this, and when I run it inside a Tomcat web app.

public static void main(String[] args)
{
    System.out.println(System.getProperty("user.language"));
    System.out.println(System.getProperty("user.country"));
    System.out.println(Locale.getDefault(Category.DISPLAY));
    System.out.println(Locale.getDefault(Category.FORMAT));

    Locale l = new Locale("de", "DE");
    System.out.println(l.getDisplayLanguage());
    DecimalFormatSymbols dfs = new DecimalFormatSymbols(l);
    System.out.println(dfs.getDecimalSeparator());

    l = new Locale.Builder().setLanguage("de").setRegion("DE").build();
    System.out.println(l.getDisplayLanguage());
    dfs = new DecimalFormatSymbols(l);
    System.out.println(dfs.getDecimalSeparator());
}

Standalone result (expected):

en
US
en_US
en_US
German
,
German
,

In Tomcat:

en
US
en_US
en_US
German
.
German
.

Can someone suggest what else influences DecimalFormatSymbols (and NumberFormat for that matter) that it doesn't use the locale provided. I'm using JDK 1.8 with language level 1.7.

EDIT: This only happens when Tomcat runs in Eclipse. Eclipse/OSGi seems to interfere with the locale-based formatting, but only in a specific situation.

like image 669
Laszlo B Avatar asked Jul 16 '14 12:07

Laszlo B


People also ask

How do you do 2 decimal places in Java?

Just use %. 2f as the format specifier. This will make the Java printf format a double to two decimal places.

Is Java DecimalFormat thread safe?

DecimalFormat isn't thread-safe, thus we should pay special attention when sharing the same instance between threads.

What is the use of DecimalFormat in Java?

You can use the DecimalFormat class to format decimal numbers into locale-specific strings. This class allows you to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.


1 Answers

Tomcat our ENV setting might have an issue i see it from the post here.

Try starting the tomcat as suggested in the above post with the following params set as a batch file:

-Duser.timezone=Europe/Berlin
-Duser.country=DE
-Duser.language=de
like image 145
Raghuveer Avatar answered Sep 23 '22 16:09

Raghuveer