Double.Parse - Internationalization problem
Asked Answered
E

6

19

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

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:

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?

If I do

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

formatted is now 0,009. Anybody?

Eer answered 6/4, 2009 at 15:22 Comment(1)
More details about this and many other issues with localization like it at moserware.com/2008/02/does-your-code-pass-turkey-test.htmlHowes
T
22

It is taking 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.

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.

Torchwood answered 6/4, 2009 at 15:26 Comment(2)
yes, I got it all wrong. Thanks guys for the answers. It was really helpful.Eer
No problem. I'm always happy to help. Sorry if my answer came across a bit sharp - it wasn't intended that way but re-reading it now, it reads like I'm a right git.Torchwood
P
10

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

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

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

Pyxie answered 19/5, 2010 at 21:25 Comment(2)
if myString is 4,5 for example, the code above will give you 45, does not work for spanish languagePupillary
no and it shouldn't. See learn.microsoft.com/en-us/dotnet/api/…: "The invariant culture is culture-insensitive; it is associated with the English language but not with any country/region.". If you know your numbers are coming in Spanish format, you should provide the corresponding CultureInfo: new CultureInfo("es-ES")Pyxie
R
4

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.

In Spanish culture, "." is a thousands separator, so "0.009" is 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.

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.

Rox answered 6/4, 2009 at 15:26 Comment(0)
M
3

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 parsed with 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.

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

Moa answered 6/4, 2009 at 15:29 Comment(0)
S
2

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.

Strake answered 6/4, 2009 at 15:27 Comment(0)
T
1
double d = Double.Parse("0,009",
    NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands,
    CultureInfo.CreateSpecificCulture("es-ES"));

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

Tetrastichous answered 6/4, 2009 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.