0

I am trying to convert a string to double but unable to do so...

I tried to simulate a dummy code which is part of my application, the text value comes from a 3rd party application on which i dont have control.

I need to convert the string which is represented in a general format "G" to a double value and show that in a text box.

        string text = "G4.444444E+16";
        double result;

        if (!double.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out result))
        {
        }

I tried by changing the numberstyles and cultureinfo but still the result is always returns 0. Suggest what is wrong with the code?

  • did you try `System.Globalization.NumberStyles.Float` ? – Rohit Feb 10 '15 at 14:17
  • 3
    Related? http://stackoverflow.com/questions/3879463/parse-a-number-from-exponential-notation - you might need to trim away the `'G'` first. – Corak Feb 10 '15 at 14:17
  • Why is that G there by the way? – virusrocks Feb 10 '15 at 14:22
  • @virusrocks - its [general format specifier](https://msdn.microsoft.com/en-us/library/dwhawy9k%28v=vs.110%29.aspx#GFormatString). Where does this string come from? if you want to parse it, it seems you will need to remove the "G", but it would be better to see if you can alter where it comes from instead – Sayse Feb 10 '15 at 14:25

2 Answers2

0

Just get rid of this "G"

    string text = "G4.444444E+16";
    string text2 = text.SubString(1); // skip first character
    double result;

    if (!double.TryParse(text2, NumberStyles.Any,
        CultureInfo.InvariantCulture, out result))
    {
    }
DrKoch
  • 9,556
  • 2
  • 34
  • 43
0

Here is more details:

        Thread.CurrentThread.CurrentCulture = new CultureInfo("ru-RU");

        // if input string format is different than a format of current culture
        // input string is considered invalid, unless input string format culture is provided as additional parameter

        string input = "1 234 567,89"; // Russian Culture format
        // string input = "1234567.89"; // Invariant Culture format

        double result = 9.99; // preset before conversion to see if it changes
        bool success = false;

        // never fails
        // if conversion is impossible - returns false and default double (0.00)
        success = double.TryParse(input, out result);
        success = double.TryParse(input, NumberStyles.Number, CultureInfo.InvariantCulture, out result);

        result = 9.99;
        // if input is null, returns default double (0.00)
        // if input is invalid - fails (Input string was not in a correct format exception)
        result = Convert.ToDouble(input);
        result = Convert.ToDouble(input, CultureInfo.InvariantCulture);

        result = 9.99;
        // if input is null - fails (Value cannot be null)
        // if input is invalid - fails (Input string was not in a correct format exception)
        result = double.Parse(input);
        result = double.Parse(input, CultureInfo.InvariantCulture);
monstro
  • 6,254
  • 10
  • 65
  • 111