从OS获取数字格式 [英] Get number format from OS

查看:106
本文介绍了从OS获取数字格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题似乎非常简单,但我很尴尬地说我无法弄清楚它是如何工作的。

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

我希望我的Java SE应用程序从操作系统中配置的任何内容中获取其数字格式。就像该操作系统上的任何其他应用程序一样。

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.

我的目标主要是Windows。在Windows中考虑此示例:

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

我用这个小例子检查结果:

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());
    }
}

通过上面的截图示例,我的小Java示例打印:

With the screenshot example above my little Java example prints:

Decimal separator is : ,
Thousands separator is : .

我原以为它会说小数点分隔符是一个点数千美元b $ b分隔符是一个逗号 - 因为这是我告诉Windows的,而这正是所有其他Windows应用程序所接受的。

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.

我认为在Windows中这些设置需要对新流程的影响。我试过重启我的IDE。没有帮助。

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

我做错了什么?

推荐答案

我自己找到了答案。

从JDK8开始,您可以要求Java在解决与语言环境相关的内容时将主机设置作为您的首要任务。

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);
    }
}

上运行我的示例 -Djava.locale.providers = HOST,JRE,SPI 给出:

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

运行不带 java.locale.providers 已定义:

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

我个人总是用<运行我的Java应用程序code> -Djava.locale.providers = HOST,.... 因为这将使我的Java应用程序在同一主机上的行为与本机应用程序相同。

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.

我在JDK8 javadocs的内部找到了答案 。在Java中还有一个关于它的一些注释 8增强文档。

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

这篇关于从OS获取数字格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆