Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
The "E" and "e" custom specifiers in C#
The "E" and "e" custom specifiers in C# are used to format numbers in scientific notation (exponential notation). When any of these specifiers appear in a format string followed by at least one zero, the number is formatted with an "E" or "e" separator between the mantissa and the exponent.
Syntax
The following format specifiers create exponential notation −
"E0" // Uppercase E with minimum exponent digits "e0" // Lowercase e with minimum exponent digits "E+0" // Uppercase E with explicit + sign "e+0" // Lowercase e with explicit + sign "E-0" // Uppercase E with - sign for negative exponents only "e-0" // Lowercase e with - sign for negative exponents only
The number of zeros after the specifier determines the minimum number of digits in the exponent −
"E0" // At least 1 exponent digit "E00" // At least 2 exponent digits "E000" // At least 3 exponent digits
Using Different E/e Specifiers
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
double num = 9400;
// Basic E and e formats
Console.WriteLine(num.ToString("0.###E0", CultureInfo.InvariantCulture));
Console.WriteLine(num.ToString("0.###e0", CultureInfo.InvariantCulture));
// With explicit + sign
Console.WriteLine(num.ToString("0.###E+0", CultureInfo.InvariantCulture));
Console.WriteLine(num.ToString("0.###e+0", CultureInfo.InvariantCulture));
// With minimum exponent digits
Console.WriteLine(num.ToString("0.###E+000", CultureInfo.InvariantCulture));
Console.WriteLine(num.ToString("0.###e+000", CultureInfo.InvariantCulture));
}
}
The output of the above code is −
9.4E3 9.4e3 9.4E+3 9.4e+3 9.4E+003 9.4e+003
Formatting Different Numbers
Example
using System;
using System.Globalization;
class Demo {
static void Main() {
double[] numbers = { 1234.567, 0.000123, -456789.12 };
foreach (double num in numbers) {
Console.WriteLine($"Number: {num}");
Console.WriteLine($"E+0: {num.ToString("0.##E+0", CultureInfo.InvariantCulture)}");
Console.WriteLine($"e-0: {num.ToString("0.##e-0", CultureInfo.InvariantCulture)}");
Console.WriteLine($"E+00: {num.ToString("0.##E+00", CultureInfo.InvariantCulture)}");
Console.WriteLine();
}
}
}
The output of the above code is −
Number: 1234.567 E+0: 1.23E+3 e-0: 1.23e3 E+00: 1.23E+03 Number: 0.000123 E+0: 1.23E-4 e-0: 1.23e-4 E+00: 1.23E-04 Number: -456789.12 E+0: -4.57E+5 e-0: -4.57e5 E+00: -4.57E+05
Key Differences Between Variants
| Format | Description | Example Output |
|---|---|---|
| E0 | Uppercase E, no explicit + for positive exponents | 1.23E4 |
| e0 | Lowercase e, no explicit + for positive exponents | 1.23e4 |
| E+0 | Uppercase E with explicit + sign | 1.23E+4 |
| E-0 | Uppercase E with - only for negative exponents | 1.23E4 or 1.23E-4 |
| E000 | Minimum 3 digits in exponent | 1.23E+004 |
Conclusion
The "E" and "e" custom specifiers in C# provide flexible formatting for scientific notation. Use uppercase "E" for formal presentations and lowercase "e" for casual display, with control over sign display and exponent digit formatting to match your specific requirements.
