If you need to run the app on several computers in different parts of the world, or with just different culture settings, you may have problems with parsing ...

In the countries of the DNS, a comma is used, in many others - a period. And parsing, respectively, in some cases simply does not work as needed. Without specifying the culture manually.

Closed due to the fact that the essence of the question is incomprehensible by the participants of the cheops , Suvitruf , iluxa1810 , Kromster , Denis 9 Nov '16 at 6:44 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    Comma is in Russian "comma". - Nick Volynkin

1 answer 1

Extension for parsing decimal numbers from string.

  • No matter the number at the beginning, middle or end of the line.
  • It does not matter There will be only a number or a bunch of "junk" characters.
  • No matter what delimiter is in the settings of Windows: it will parse anyone to a point anyway.
  • The ability to set a delimiter manually.

    public static class StringExtension { public static double DoubleParseAdvanced(this string strToParse, char decimalSymbol = ',') { string tmp = Regex.Match(strToParse, @"([-]?[0-9]+)([\s])?([0-9]+)?[." + decimalSymbol + "]?([0-9 ]+)?([0-9]+)?").Value; if (tmp.Length > 0 && strToParse.Contains(tmp)) { var currDecSeparator = System.Windows.Forms.Application.CurrentCulture.NumberFormat.NumberDecimalSeparator; tmp = tmp.Replace(".", currDecSeparator).Replace(decimalSymbol.ToString(), currDecSeparator); return double.Parse(tmp); } return 0; } } 

How to use:

 "It's 4.45 O'clock now".DoubleParseAdvanced(); // возвратит 4.45 "It's 4,45 O'clock now".DoubleParseAdvanced(); // возвратит 4.45 "It's 4:45 O'clock now".DoubleParseAdvanced(':'); // возвратит 4.45