Search code examples
c#number-formattingformat-specifiers

how can I remove zeros from exponent notation


I'm using exponential formatting to format a decimal number in C#. For example if the number is

0.0001234567

Formatting with

(0.0000123456).ToString("E4");

Shows

1.2345E-004

How can I remove leading zero from exponent so it read as below?

1.2345E-4

Solution

  • Assuming you need to always show 4 digits after decimal point, try

    "0.0000E+0"
    

    so it will show

    (0.0000123456).ToString("0.0000E+0"); //1.2345E-5 
    (0.0000120000).ToString("0.#E+0");    //1.2000E-5
    

    if you don't need to show 4 digits after decimal points use

    "0.#E+0"
    

    so it will show

    (0.0000123456).ToString("0.#E+0"); //1.2E-5
    (0.0000120000).ToString("0.#E+0"); //1.2E-5