Search code examples
c#doublenotationexponent

Double value to scientific notation with fixed exponent C#


I am having value double value = 1427799000;

I would like to convert it to scientific notation where the values exponent must always 10^11 (E+11).

I have tried following but it is not working.

Console.WriteLine(value.ToString("00.##E+11", CultureInfo.InvariantCulture));

Output should be : 0.14 x 10^11 or 0.14E+11

How to convert any double value to scientific notation with fixed exponent ? Here fixed exponent is 11.


Solution

  • double value = 1427799000;
    Console.WriteLine(value.ToString("G2", CultureInfo.InvariantCulture));
    
    //output: 1.4E+09
    

    The General ("G") Format Specifier

    The general ("G") format specifier converts a number to the most compact of either fixed-point or scientific notation, depending on the type of the number and whether a precision specifier is present.

    EDIT: About your comment you can't display the scientific notation in your desirable way, it is not defined that way ! The coeficient must be greater or equal to 1 and less to 10.

    For number 1.23*10^11 ->Article source

    The first number 1.23 is called the coefficient. It must be greater than or equal to 1 and less than 10.

    The second number is called the base . It must always be 10 in scientific notation. The base number 10 is always written in exponent form. In the number 1.23 x 10^11 the number 11 is referred to as the exponent or power of ten.