C# Double.Parse - 国际化问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/721950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:30:41  来源:igfitidea点击:

Double.Parse - Internationalization problem

c#stringdoubleculturecultureinfo

提问by oz.

This is driving me crazy. I have the following string in a ASP.NET 2.0 WebForm Page

这真让我抓狂。我在 ASP.NET 2.0 WebForm 页面中有以下字符串

string s = "0.009";

Simple enough. Now, if my culture is Spanish - which is "es-ES" - and I try to convert the string to Double, I do the following:

足够简单。现在,如果我的文化是西班牙语 - 即“es-ES” - 我尝试将字符串转换为 Double,我执行以下操作:

double d = Double.Parse(s, new CultureInfo("es-ES"));

what I'd expect is 0,009. Instead, I get 9. I understand that .NET thinks it is a thousand separator, which in en-US is a comma, but shouldn't it take the culture info I'm passing to the parse method and apply the correct format to the conversion?

我期望的是 0,009。相反,我得到了 9。我知道 .NET 认为它是千位分隔符,在 en-US 中它是一个逗号,但它不应该采用我传递给 parse 方法的文化信息并将正确的格式应用于转换?

If I do

如果我做

double d = 0.009D;
string formatted = d.ToString(new CultureInfo("es-ES"));

formatted is now 0,009. Anybody?

格式化现在是 0,009。有人吗?

采纳答案by Jeff Yates

It istaking the culture you gave and applying the correct formatting. You provided a string of "0.009" and told it that it was Spanish...then you complain that it properly interpreted it as Spanish! Don't tell it that the string is Spanish when you know it isn't.

考虑你给文化和运用正确的格式。您提供了一串“0.009”并告诉它它是西班牙语……然后您抱怨它正确地将其解释为西班牙语!当你知道它不是西班牙文时,不要告诉它字符串是西班牙文。

You should pass the Parse method the culture of the string being parsed, which in this case would be en-US or en-Gb or InvariantCulture.

您应该将要解析的字符串的区域性传递给 Parse 方法,在这种情况下将是 en-US 或 en-Gb 或 InvariantCulture。

回答by Reed Copsey

You have it backwards.

你把它倒过来了。

When you say double d = Double.Parse(s, new CultureInfo("es-ES"));, you are asking .NET to parse your string into a double, assuming that the string is written in the es-ES culture.

当您说 时double d = Double.Parse(s, new CultureInfo("es-ES"));,您是在要求 .NET 将您的字符串解析为双精度值,假设该字符串是以 es-ES 文化编写的。

In Spanish culture, "." is a thousands separator, so "0.009" is 9.

在西班牙文化中,“.” 是千位分隔符,所以“0.009”是 9。

When you convert using ToString(), at the end, it's saying convert 0.009 to a string using the spanish culture, so it uses "," as the decimal separator, and you get "0,009". The behavior is correct.

当您使用 ToString() 进行转换时,最后会说使用西班牙文化将 0.009 转换为字符串,因此它使用“,”作为小数点分隔符,您会得到“0,009”。行为是正确的。

My guess is that you want to use Double.Parse with the Invariant Culture, and ToString with the spanish culture, so 0.009 becomes 0,009.

我的猜测是,您希望将 Double.Parse 与 Invariant Culture 一起使用,并将 ToString 与西班牙文化一起使用,因此 0.009 变为 0,009。

回答by Joel Coehoorn

You're mistaking parsing and formatting. You get 9 instead of .009 the first time because you take a string that is formated in a .-based culture and parse it using a ,-based culture. You need to parse it using whatever culture it was created with and then format it using whatever culture you want for display.

你错误地解析和格式化。您第一次得到 9 而不是 .009,因为您采用基于 . 的区域性格式的字符串并使用基于 , 的区域性对其进行解析。您需要使用创建它的任何文化来解析它,然后使用您想要显示的任何文化对其进行格式化。

回答by Steve

I think you are interpreting it the wrong way around, in es-ES culture 0.009 is really just a long way of saying 9, as the "." is not the decimal separator, so if you ask for the string "0.009" to be parsedwith the es-ES culture you should indeed get the deouble 9.0. If you ask it to parse "0,009" you should get a double of 0.009.

我认为您对它的解释是错误的,在 es-ES 文化中,0.009 实际上只是将 9 称为“。” 不是十进制分隔符,因此如果您要求使用 es-ES 文化解析字符串“0.009”,您确实应该得到 deouble 9.0。如果您要求它解析“0,009”,您应该得到 0.009 的两倍。

Similarly, if you ask it to formatthe double 0.009 you should get the string "0,009" in es-ES.

同样,如果您要求它格式化双 0.009,您应该在 es-ES 中得到字符串“0,009”。

回答by Konstantin Tarkus

double d = Double.Parse("0,009",
    NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
    CultureInfo.CreateSpecificCulture("es-ES"));

In es-ES culture "," is a decimal seporator (not ".")

在 es-ES 文化中,“,”是十进制分隔符(不是“.”)

回答by Nicolas78

what Jess's writing works for me. just for anyone who'd need to try out how to get "invariant culture": it looks this

Jess 的作品对我有用。仅适用于需要尝试如何获得“不变文化”的任何人:看起来像这样

double d = Double.Parse(myString, CultureInfo.InvariantCulture);

double d = Double.Parse(myString, CultureInfo.InvariantCulture);

(first stackoverflow post, so yea, rather marginal ;)

(第一个stackoverflow帖子,所以是的,相当边缘;)