Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

double.TryParse in dutch

Tags:

c#

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"

like image 829
Ervin Ter Avatar asked May 25 '09 07:05

Ervin Ter


2 Answers

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);
like image 165
Matt Brindley Avatar answered Sep 27 '22 21:09

Matt Brindley


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 .

like image 41
Peter Stuer Avatar answered Sep 27 '22 22:09

Peter Stuer