none
Convert string to double without scientific notation RRS feed

  • Question

  • Hi,

    I think I'm gonna kill myself :)

    I just want to convert a string number to double, such as "0.2" to 0.2

    Convert.ToDouble() OR double.TryParse() works perfectly with almost all numbers :

    var str = "0.2" var newdouble = Converter.ToDouble(str.Replace(".", ","));

    var str = "0.2"
    double number;
    var newdouble = double.TryParse(str, NumberStyles.Number, CultureInfo.InvariantCulture, out number);

    This is perfect. But when it reaches to more complex number such as: "0.00000000001"

    The result is : 1E-09.00

    AND I DON'T ****** want that ! I just want a simple conversion : 0.00000000001

    Please... help...

    Thank you


    • Edited by Valgoku Wednesday, June 20, 2018 9:16 AM
    Wednesday, June 20, 2018 9:15 AM

Answers

  • You probably see it in Debugger windows (Watch, Autos). If you execute ‘double newdouble = 0.00000000001’ you will see the same result, because the behaviour is not caused by the converter. It is just a display format chosen by the Debugger.

    In order to see the desired result, try entering this expression into Watch window:

       newdouble.ToString("f99").TrimEnd('0'),nq


    • Marked as answer by Valgoku Wednesday, June 20, 2018 3:19 PM
    Wednesday, June 20, 2018 10:50 AM

All replies

  • You probably see it in Debugger windows (Watch, Autos). If you execute ‘double newdouble = 0.00000000001’ you will see the same result, because the behaviour is not caused by the converter. It is just a display format chosen by the Debugger.

    In order to see the desired result, try entering this expression into Watch window:

       newdouble.ToString("f99").TrimEnd('0'),nq


    • Marked as answer by Valgoku Wednesday, June 20, 2018 3:19 PM
    Wednesday, June 20, 2018 10:50 AM
  • Yes you are right.

    If you just try to ToString(CultureInfo.InvariantCulture) this new double, it displays the debugged number..

    Your answer solved my problem.

    For those who don't want too many decimals, you can do a function which allows you to specify the number of decimals you want :)

            public static string ConvertDoubleToString(double numberToConvert, int numberOfDecimals = 99)
            {
                // example F99: fixed-point format specifier : with 99 decimals
                // https://msdn.microsoft.com/fr-fr/library/system.globalization.numberformatinfo(v=vs.110).aspx
                return numberToConvert.ToString("F" + numberOfDecimals, CultureInfo.InvariantCulture);
            }

    Thank you Viorel_ !

    • Edited by Valgoku Wednesday, June 20, 2018 3:19 PM
    • Marked as answer by Valgoku Wednesday, June 20, 2018 3:19 PM
    • Unmarked as answer by Valgoku Wednesday, June 20, 2018 3:19 PM
    • Marked as answer by Valgoku Wednesday, June 20, 2018 3:19 PM
    • Unmarked as answer by Valgoku Wednesday, June 20, 2018 3:20 PM
    Wednesday, June 20, 2018 3:18 PM