Reputation: 1357
I download one program that read file and then parse double values from String to Double. But I get an exception because this file contains numbers with '.' separator, but there is ',' in my culture. How can I set culture explicitly?
Upvotes: 1
Views: 3026
Reputation: 21
this was i'm used to do but i think i will use the NumberFormatInfo in the future !
CultureInfo oldCulture = Thread.CurrentThread.CurrentCulture;
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
try
{
if (Thread.CurrentThread.CurrentCulture != null)
{
double d = Double.Parse("23.5");
}
}
finally
{
Thread.CurrentThread.CurrentCulture = oldCulture;
}
Upvotes: 2
Reputation: 2377
Also usable:
double.Parse((""+s).Replace(",","."), System.Globalization.CultureInfo.InvariantCulture)
Ugly as hell, but that's .Net... :)
Upvotes: 0
Reputation: 57688
You would use the Parse
overload that accepts an IformatProvider
.
Double.Parse("23.56", new CultureInfo("..."))
If you don't know the culture used to write the file you create a NumberFormatInfo
and configure it as you like:
var nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ".";
var d = Double.Parse("23.56", nfi);
Upvotes: 9