Web server running in Dutch(Belgium)
double output;
double.TryParse(txtTextbox1.Text, out output);
Is this a good way to convert text to double in dutch environment? Let's say the input is "24.45" instead of "24,45"
If you want to use the Dutch (Belgium) number format:
double output;
double.TryParse("24,45", NumberStyles.Any, CultureInfo.GetCultureInfo("nl-BE"), out output);
Or to use the US number format:
double output;
double.TryParse("24.45", NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out output);
If you attempt to parse "24.45" with a Dutch culture set, you'll get back "2445", similarly, if you attempt to parse "24,45" with a US culture, you'll get "2445". If you want the parse to fail if the wrong decimal point is used, change NumberStyles.Any to exclude the flag: NumberStyles.AllowThousands
:
double output;
if (double.TryParse("24.45", NumberStyles.Any ^ NumberStyles.AllowThousands, CultureInfo.GetCultureInfo("nl-BE"), out output))
If your entire application is in Dutch, you should change your cultureinfo globally - here's how to do it in WinForms and here's how to do it in ASP.NET.
Once you're using a globally set CultureInfo, you can change the above code to:
double output;
double.TryParse("24.45", NumberStyles.Any, CultureInfo.CurrentCulture, out output);
The correct Culture Code for dutch-Belgium is "nl-BE", so you should use that instead of the often suggested "nl-NL", which would give you the variant of Dutch culture settings appropriate for the Netherlands.
double output;
double.TryParse("24.45", NumberStyles.Any, CultureInfo.GetCultureInfo("nl-BE"), out output);
You can find a complete list of Culture Codes at http://arvindlounge.spaces.live.com/blog/cns!C9061D5B358A2804!263.entry .
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