I'm experiencing weird behavior when converting a number to a double, when using culture information.
When converting "3,3"
using the Dutch culture, is handled correctly. If I convert "3,3"
using the US culture, it returns 33
. I was expecting an error. See my example:
static void Main(string[] args)
{
CultureInfo cultureDutch = new CultureInfo("nl-NL");
CultureInfo cultureUS = new CultureInfo("en-US");
System.Threading.Thread.CurrentThread.CurrentCulture = cultureDutch;
Console.WriteLine("Input 3,3 --> Expected 3,3");
Console.WriteLine("Output = " + Convert.ToDouble("3,3", cultureDutch));
// Actual result --> 3,3
Console.WriteLine("Input 3,3 --> Expected InvalidCastException");
Console.WriteLine("Output = " + Convert.ToDouble("3,3", cultureUS));
// Actual result --> 33
Console.WriteLine();
Console.WriteLine();
System.Threading.Thread.CurrentThread.CurrentCulture = cultureUS;
Console.WriteLine("Input 3.3 --> Expected InvalidCastException");
Console.WriteLine("Output = " + Convert.ToDouble("3.3", cultureDutch));
// Actual result --> 33
Console.WriteLine("Input 3.3 --> Expected 3.3");
Console.WriteLine("Output = " + Convert.ToDouble("3.3", cultureUS));
// Actual result --> 3.3
Console.ReadLine();
}
What is the correct way to handle this? I would prefer an exception when a decimal (or thousand) separator is invalid.
If you just want to parse it I would use the dedicated parse methods in which you can set Numberstyles
The following code would throw a FormatException
var culture =new CultureInfo("en-US");
var result = double.Parse("3,3", NumberStyles.AllowDecimalPoint, culture);
For further information see Double.Parse Method
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With