Java:sv和sv_SE语言环境有什么区别? [英] Java: what's the difference between the sv and sv_SE locale?

查看:394
本文介绍了Java:sv和sv_SE语言环境有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I tried to parse a string (-0,3) to a double in java using Swedish locale using this code:

我尝试使用瑞典语语言环境解析一个字符串(-0.3) > String DoubleString =-0,3
NumberFormat swedishNumberFormat = NumberFormat.getInstance(new Locale(sv));
System.out.println(swedishNumberFormat.parse(doubleString).doubleValue());

String DoubleString = "-0,3" NumberFormat swedishNumberFormat = NumberFormat.getInstance(new Locale("sv")); System.out.println(swedishNumberFormat.parse(doubleString).doubleValue());

当我尝试使用 sv_SE 语言环境结果为-3.0,显然是不正确的。然后,在很多头痛之后,将语言环境更改为 sv (如上例),然后结果正确,-0, 3。

When I tried with the sv_SE locale the result was -3.0, which, obviously, is incorrect. I then, after a lot of headache changed the locale to sv (as in the example above) and then the result was correct, -0,3.

根据 http: //www.localeplanet.com/java/sv/index.html http://www.localeplanet.com/java/sv-SE/index.html 在这两种情况下的小数分隔符(Decimalavgränsare)是逗号()所以为什么这个差异?

According to http://www.localeplanet.com/java/sv/index.html and http://www.localeplanet.com/java/sv-SE/index.html the decimal separator (Decimalavgränsare) in both cases is comma (,) so why this difference?

(我试图用一些与瑞典相关的标签来标记这个标签,如sv_SE,但是我没有足够的声望点添加新标签如果可以,请随意添加标签。)

(I tried to tag this with some tags related to Sweden such as sv_SE but I didn't have enough reputation points to add new tags. Feel free to add tags if you can.)

推荐答案

如果您尝试创建 Locale like new Locale(sv_SE),那么它将无法通过用于检测语言的ISO 639标准是的,如果只有一个参数是thre作为语言,检查文档)。在您的情况下,您应该使用新的区域设置(sv,SE)正确设置国家/地区,否则由于语言匹配失败,使用默认语言在你的情况下,似乎是英文)。在英文中,逗号用于数字分组,而不是小数点,这就是为什么结果出乎意料之外但正确。

If you try to create Locale like new Locale("sv_SE"), then it will fail to match it through the ISO 639 standard used to detect language (yes if there is only one parameters it is threatened as language, check the documentation). In your case you should use new Locale("sv","SE") to correctly setup the country, otherwise, since the matching for language fails, default language is used (which in your case seem to be English). In English, the comma is used for digit grouping and not as decimal mark, that is why the result you have in unexpected but correct.

这篇关于Java:sv和sv_SE语言环境有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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