Search code examples
c#doublescientific-notation

How to convert scientific notation double to non scientific notations double


I have a double that shows scientific notations like this: 2.95E-05 I get those values from a JSON string as double directly. I wonder how the double can be converted so it instead shows all zeros like a normal number? (0.0000295)

Below attempt does not work and still shows: 2.95E-05

Thank you!

 double value = 2.95E-05;
 value = double.Parse(value.ToString(), System.Globalization.NumberStyles.Any);
 MessageBox.Show(value.ToString());

Solution

  • There are several ways to do this and it depends on whether you want fixed precision or padded with 0's.

    double value = 2.95E-05;
    value = double.Parse(value.ToString(), System.Globalization.NumberStyles.Any);
    Console.WriteLine(string.Format("{0:F10}",value));
    Console.WriteLine($"{value:F10}");
    Console.WriteLine(value.ToString("F10}"));
    Console.WriteLine($"{value:0.##########}");
    

    Please take a look at the following to ascertain how to format numeric values, it's all you need to know and more. Although there is a lot of information here, the easiest way is too look at the examples: