Exponential ("E") Format Specifier In C#

Introduction

The Format specifier can be used to convert the number into the string in the form  "-d.ddd..E+ddd" or "-d.ddd..e+ddd", where d represents any digits between 0 to 9 and the negative sign represents the negative number. At least one digit exists before the decimal point. After the decimal point the desired number of digits exist. The default of 6 digits exists after the decimal point if the precision specifier does not exist. The exponent prefix is indicated by "E" or "e" , and the exponent always consists of a plus or minus sign or the minimum of three digits.
The exponent is padded with zeros to meet this minimum, if required. The resulting string is affected by the formatting information of the current NumberFormatInfo class's object, or we can say that this format specifier can be used to convert any number into the exponential form; in other words "any number is multiplied by ten to the power of any number". Now write the following code for determine the exponential format specifier in the console application:

namespace NumberFormatting

{

    class Program

    {

        static void Main(string[] args)

        {

            double value = 12345.6789;

            double value1 = 123475.6789;

            //represents the exponential value with the power of 4.

            Console.WriteLine("Exponential power is four that is 004 after the exponent e");

            Console.WriteLine(" The exponential value is : " + value.ToString("e", CultureInfo.InvariantCulture));

            Console.WriteLine(" The exponential value is : " + value.ToString("E", CultureInfo.InvariantCulture));

            //represents the exponential value with the power of 5.

            Console.WriteLine("Exponential power is five that is 005 after the exponent e");

            Console.WriteLine(" The exponential value is : " + value1.ToString("e", CultureInfo.InvariantCulture));

            Console.WriteLine(" The exponential value is : " + value1.ToString("E", CultureInfo.InvariantCulture));

        }

    }

}

Output

Exponential-format-specifier-output-in-csharp.jpg

Number of digits increase or decrease in exponential format specifier

If we want the number of digits before the exponential (represented the letter "E" or "e") to be increased or decreased then that depends on how we specify this format; see:
 

namespace NumberFormatting

{

    class Program

    {

        static void Main(string[] args)

        {

            double value = 12345.6789;

            //display the exponential number string with 10 digits before the exponent value.If the digits in the number that we specify is less than the number of digits that

            //we want in the output then it will give zero after the number, and give the result according to our need.

            Console.WriteLine(value.ToString("Exponent Number String with ten digit : " + value.ToString("E10", CultureInfo.InvariantCulture)));

            Console.WriteLine(value.ToString("Exponent Number String with ten digit :" + value.ToString("e10", CultureInfo.InvariantCulture)));

            //Display number with four digits before the exponent value

            Console.WriteLine(value.ToString("Exponent Number String with four digit : " + value.ToString("E4", CultureInfo.InvariantCulture)));

            Console.WriteLine(value.ToString("Exponent Number String with four digit : " + value.ToString("e4", CultureInfo.InvariantCulture)));

        }

    }

}

Output

Number-of-digit-specified-in-exponential-format-specifier-in-asharp.jpg

Display Exponential Value with different Culture
 

namespace NumberFormatting

{

    class Program

    {

        static void Main(string[] args)

        {

            double value = 12345.6789;

            //the fr-FR culture will display the exponent number with comma in place of decimal point

            Console.WriteLine("Display the exponent value with fr-FR Culture : " + value.ToString("E", CultureInfo.CreateSpecificCulture("fr-FR")));

            //the en-US culture will display the exponent number with  decimal point

            Console.WriteLine("Display the exponent value with en-US Culture : " + value.ToString("E", CultureInfo.CreateSpecificCulture("en-US")));

        }

    }

}

Output

Display-exponential-number-with-different-culture-format.jpg

NumberDecimalSeparator property

This property is used to separate the number with the decimal point, this is the property of the NumberFormatInfo class so we create the object of this class to apply this property to the exponential format specifier as:
 

namespace NumberFormatting

{

    class Program

    {

        static void Main(string[] args)

        {

            // Gets a NumberFormatInfo associated with the en-US culture.

            NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;

            // Displays a value with the default separator (".").

            Int64 myInt = 123456789;

            Console.WriteLine("The exponential number string is  :\n " + myInt.ToString("E", nfi));

            // Displays the same value with a blank as the separator.

            nfi.NumberDecimalSeparator = " ";

            Console.WriteLine("The exponential number string when apply the decimal Separation in this  : \n " +myInt.ToString("E", nfi));

        }

    }

}

Output

Number-decimal-separation-in-exponential-format-specifier.jpg

Summary

In this article I explained how to use the Exponential Format Specifier In C#.

Up Next
    Ebook Download
    View all
    Learn
    View all