Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
226 views
in Technique[技术] by (71.8m points)

java - Get number format from OS

This question seems insanely simple, yet I'm embarrased to say I haven't been able to figure out how it works.

I want my Java SE application to get its number format from whatever is configured in the OS. Just like any other application on that OS.

My target is primarily Windows. Consider this example in Windows:

enter image description here

I use this little example to check the result:

public class MyMain {
    public static void main(String[] args) {
        DecimalFormat decFormat = new DecimalFormat();
        DecimalFormatSymbols decSymbols = decFormat.getDecimalFormatSymbols();           
        System.out.println("Decimal separator is : " + decSymbols.getDecimalSeparator());
        System.out.println("Thousands separator is : " + decSymbols.getGroupingSeparator());
    }
}

With the screenshot example above my little Java example prints:

Decimal separator is : ,
Thousands separator is : .

I would have expected it to say that the decimal separator is a dot and the thousands separator is a comma - because that's what I've told Windows and that's what all other Windows applications pick up.

I'm under the impression that in Windows these settings take effect for new processes. I've tried restarting my IDE. Didn't help.

What am I doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've found the answer myself.

Starting from JDK8 you can ask Java to have the host's settings as your number one priority when resolving locale related stuff.

I've extended my example slightly:

public class MyMain {
    public static void main(String[] args) {
        DecimalFormat decFormat = new DecimalFormat();
        DecimalFormatSymbols decSymbols = decFormat.getDecimalFormatSymbols();
        String localeProvidersList = System.getProperty("java.locale.providers", "JRE,SPI");
        System.out.println("Decimal separator is : " + decSymbols.getDecimalSeparator());
        System.out.println("Thousands separator is : " + decSymbols.getGroupingSeparator());
        System.out.println("Locale providers list : " + localeProvidersList);
    }
}

Running my example above with -Djava.locale.providers=HOST,JRE,SPI gives:

Decimal separator is : .
Thousands separator is : ,
Locale providers list : HOST,JRE,SPI

Running without java.locale.providers defined gives:

Decimal separator is : ,
Thousands separator is : .
Locale providers list : JRE,SPI

Personally I would always run my Java applications with -Djava.locale.providers=HOST,.... because that will make my Java applications behave like native application do on that same host.

I found the answer deep inside the JDK8 javadocs. There's also a few notes about it in the Java 8 Enhancements docs.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...