| by Arround The Web | No comments

Exponential (“E”) Format Specifier in C#

In computing, numerical data formatting is a common activity. The way users present statistics can affect how they are interpreted, whether or not users are dealing with financial information, scientific computations, or any other field where accuracy and readability are essential. In C#, the exponential (“E”) format specifier is one of the useful tools which is used to format the numerical values.

In this guide, we will illustrate the “E” format specifier in C#.

What is an Exponential (“E”) Format-Specifier in C#?

In C#, the “E” is used as a specifier for the exponential format which is applied to scientifically format a numerical number. When dealing with large or small integer values, it is challenging to represent in conventional decimal or fixed-point notation, in that situation, it is frequently applied. A number is transformed into a string of the form “-d.ddd…E+ddd” or “-d.ddd…e+ddd,” where each “d” stands for a digit (0–9), using an exponential (“E”) format specifier.

Syntax

The basic syntax of the above-mentioned format specifier is as follows:

value.ToString("E")

 

In the above syntax, the “value” represents a numerical digit that is passed to the method “ToString()”, whereas “E” defines the exponential notation.

Example: Using Exponential (“E”) Format Specifier to Display Given Number in C#

To understand the usage of the “E” format specifier, let’s examine the following program. First, we import the “using System” header type for accessing the type and members in the System namespace:

using System;

 

In the below snippet code, initially define a “Program” class that invokes the “Main()” method to start the execution. Then, initialize the variable double type “number” variable with “321456.789”. After this, apply the “To.String()” method that takes an “E” as a parameter which gets the “number” and converts it into the string format. Next, assign the result to the variable “formattedNumber”. Finally, used the “Console.WriteLine()” method to print the message on the terminal:

public class Program
{
    public static void Main(string[] args)
    {
        double number = 321456.789;
string formattedNumber = number.ToString("E");

Console.WriteLine("The "+number+ " using E format Specifier is: "+formattedNumber);
    }
}

 

When the above-described code will be executed, it will show the following output:

Note: In the above example, the value of a number is represented in scientific notation with a coefficient of “1.234568” and a power of “10” raised to the 5th (1.234568 × 10^5). The “E+05” component shows that a decimal point has been relocated to the right “5” places.

Example 2: Using Exponential (“E”) Format Specifier to Display Small and Larger Numbers in C#

Here is another example that uses a format specifier (“E”) to print small and large numbers.

In the below code, inside the “Main()” function, two variables “large_Number” and “small_Number” are initialized with “123456789.12345” and “0.0000123456789” values of “double” data type. Then, create strings in exponential format from these integers using the “ToString()” method together with the “E” format specifier. At last, to display the outcome of both variables, apply the “Console.WriteLine()” function:

using System;
class Code
{
    static void Main()
    {
        double large_Number = 123456789.12345;
        double small_Number = 0.0000123456789;

        Console.WriteLine("Large Number: " + large_Number.ToString("E"));
        // Console.WriteLine("\n");
        Console.WriteLine("Small Number: " + small_Number.ToString("E"));
    }
}

 

Th output will be:

Note: In the above-given code, the “large_Number” is shown in the (exponential notation as “1.234568E+008“), which denotes that its decimal point has been shifted by eight positions to the right. The point of the decimal is five digits to the left, as shown in “small_Number” as “1.234568E-005“.

Conclusion

In C#, the exponential (“E”) format specifier is a useful tool for representing the integer values in scientific notation including the quick and uniformly expressing large or small values. It gives users code flexibility and readability. This guide illustrated the exponential (“E”) format specifier in C#.

Share Button

Source: linuxhint.com

Leave a Reply