double.Parse comma seperator indep.

  • Thread starter Thread starter Jesper Denmark
  • Start date Start date
J

Jesper Denmark

Hi,

I use the double.Parse functionality to convert a number
in a text file into a double. However, while this works
fine on one computer it doesn't on another. I've found
out that it is dependent on the comma seperator settings
on the computer. Is there a way to override this - I
would like double.Parse to always use a . as comma
seperator no matter what the system settings are.

Thanks
Jesper
 
Well, what you WANT it to do is irrelevant, Parse is SUPPOSED to obey system
settings.

Use another convertion means (like te Convert class), or set the thread's
culsture settings explicitly.

Thomas Tomiczek
THONA Software & Consulting Ltd.
(Microsoft MVP C#/.NET)
 
Well, Thomas could be right, but I thought you could go:

double.Parse("<your string to parse here>", CultureInfo.InvariantCulture)

which would use the settings that you used when you wrote the code and not
the user's cultural settings. I could be wrong, but if that doesn't work
you might look into the IFormatProvider interface.

Hope this helps,
Jacob
 
Jesper Denmark said:
I use the double.Parse functionality to convert a number
in a text file into a double. However, while this works
fine on one computer it doesn't on another. I've found
out that it is dependent on the comma seperator settings
on the computer.

By "comma separator settings" do you actually mean the decimal
separator?
Is there a way to override this - I
would like double.Parse to always use a . as comma
seperator no matter what the system settings are.

Use the form of Double.Parse which takes an IFormatProvider, and give
it CultureInfo.InvariantCulture, eg:

double d = Double.Parse (myString, CultureInfo.InvariantCulture);
 
Thomas Tomiczek said:
Well, what you WANT it to do is irrelevant, Parse is SUPPOSED to obey system
settings.

Well, the version of Parse which doesn't take an IFormatProvider is...
Use another convertion means (like te Convert class), or set the thread's
culsture settings explicitly.

Changing the thread's culture setting sounds like a really bad idea to
me - just passing in the appropriate culture to the overload of Parse
which takes an IFormatProvider is far cleaner, IMO.
 
Back
Top