0

Hi guys my problem is this. I made a software in c# that is able to read and edit dxf files, I have to give this software to an american company but I have discovered the following problem:

Where I live we use the ',' to separate the integer part from the decimal part of the number (for example: 2,3658) but in the USA they use the '.' so they write 2.3658. When I try to read the string "2.3658" and convert it into a double with "Double.Parse("2.3658")" the double I get is 23658 like the method "Parse()" didn't recognised the decimal part. I have found the following solution:

 UpdatedCoorx = double.Parse(shiftedE[w + 1] ,NumberStyles.Number,CultureInfo.CreateSpecificCulture ("en-US"));

Using CultureInfo.CreateSpecificCulture ("en-US") the c# can read the numbers correctly. My question is : is there a way that make c# automatically recognised the "Culture" of the pc where is installed so that it can read the number correctly???

  • 1
    Do you want it to parse in right format for the installed locale of the PC, or do you want it to parse with a consistent format which is unrelated to the PC's locale? Your question seems to be asking for both. – Will Dean Sep 17 '15 at 07:22
  • hi thanks for the answer I want to parse in right format for the installed locale of the pc. – Alessio Il Pierce Giannelli Sep 17 '15 at 07:33
  • for example if the pc where the software is installed uses the en-USA format I want the software to automatically be able to understand that 2.36 mean that "2" is the integer part and "36" is the decimal part(When I parse the string) but if the software is installed in Italy for exmple and I parse 2,36 I want the software to be able to Parse the number correctly as well... – Alessio Il Pierce Giannelli Sep 17 '15 at 07:34
  • Parse data typed by the user using CurrentCulture. Store and retrieve data using InvariantCulture. (XML serialization of data automatically uses InvariantCulture by default.) – Matthew Watson Sep 17 '15 at 07:38
  • @matthew Watson: so if I use CurrentCulture the c# uses the culture of the pc where the software is installed right??? – Alessio Il Pierce Giannelli Sep 17 '15 at 07:48
  • @AlessioIlPierceGiannelli Yep, that's correct. – Matthew Watson Sep 17 '15 at 08:53

3 Answers3

3

is there a way that make c# automatically recognised the "Culture" of the pc where is installed so that it can read the number correctly?

That's what it's doing by default - and why you're having a problem, because the culture used to created of the value you're parsing ("2.3658") isn't the culture on your local machine.

For any particular value, you should really know which culture produced it. For machine-to-machine communication, it's best to use the invariant culture (CultureInfo.Invariant) which is mostly similar to the US. Ideally, you shouldn't store values in a culture-specific format at all; either store them in a binary representation instead of a string, or if you must store a string, treat that as effectively machine-to-machine communication.

If you're in the unfortunate position of receiving data where you know it's been formatted according to some human culture, but you don't know which one, you should probably use some heuristics to detect what the culture is. That can easily fail though - for example, is "1,234" meant to be a 1 followed by a "grouping" separator, followed by 234 - meaning one thousand, two hundred and thirty-four... or is it meant to be a 1 followed by a decimal separator, making the value just a bit more than 1? Both interpretations are valid, depending on the culture you use...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • yeah my problem is that I wanna be sure that if I am in usa and I parse a String from the user like "2.3" the software knows that 2 is the integer and 3 is the decimal part but if the software is used in an other country where they use the "," as separator and I parse "2,3" the software must understand that "2" is the integer part and "3" is the decimal part??? how can I make it do it automatically??? – Alessio Il Pierce Giannelli Sep 17 '15 at 07:43
  • @AlessioIlPierceGiannelli: Then specify `CultureInfo.InvariantCulture` when parsing: `double.Parse(shiftedE[w + 1], CultureInfo.InvariantCulture)`. That's *not* using "the culture of the PC where is installed" though... it's using the invariant culture. – Jon Skeet Sep 17 '15 at 07:44
0

If you want to detect the Culture, you should be able to do so with this As explained in the link CultureInfo.CurrentCulture returns the Culture from the Windows GetUserDefaultLocaleName function.

kevintjuh93
  • 1,010
  • 7
  • 22
0

Jon's answer is pretty spot on (as usual). I just wanted to add that if you are developing an application that will be used by people in another country, it may be helpful for you to use CultureInfo.DefaultThreadCurrentUICulture while you are in development mode: just assign your users' culture to this property when the application starts, and you will have exactly the same usage experience (culture-related-things-wise) that your users will have. (Just remember to remove this when you ship your application! You could for example use a #if debug block)

Community
  • 1
  • 1
Konamiman
  • 49,681
  • 17
  • 108
  • 138