Thursday, May 27, 2021

C# - How parse double from string with specific char for decimal delimiter

If in your culture is used comma char (",") as decimal delimiter, it could appears problem, when double stored in string is delimited with classic decimal point ("."). 

 In this situation you must specify that decimal point as used delimiter. 

 So, if you have this string value "135.009995"; for right parsing you must define this code:
var currentCulture = System.Globalization.CultureInfo.InstalledUICulture;
var numberFormat = (System.Globalization.NumberFormatInfo) currentCulture.NumberFormat.Clone(); 
numberFormat.NumberDecimalSeparator = ".";

double value = 0;
bool b = Double.TryParse ( values[1], System.Globalization.NumberStyles.Any, 
                           numberFormat, out value ); 
if ( b ) el.Open = value;
Now, value contains good double value:

No comments:

Post a Comment