To show a number in scientific notation, such as 1.23E+4 instead of 12345.67890123, pass a format string to ToString. .NET offers two ways: the standard E specifier, which is quick and has a fixed layout, and custom patterns such as 0.##E+0, which let you control every digit.
All output below uses CultureInfo.InvariantCulture, so it is the same on every machine. Without it, the decimal separator follows the current culture; more on that below.
Standard and custom formats
using System.Globalization;
var inv = CultureInfo.InvariantCulture;
double number = 12345.67890123;
Console.WriteLine(number.ToString("E", inv)); // 1.234568E+004
Console.WriteLine(number.ToString("E2", inv)); // 1.23E+004
Console.WriteLine(number.ToString("e2", inv)); // 1.23e+004
Console.WriteLine(number.ToString("0.##E+0", inv)); // 1.23E+4
Console.WriteLine(number.ToString("0.##E+00", inv)); // 1.23E+04
Console.WriteLine(number.ToString("0.###E0", inv)); // 1.235E4
Console.WriteLine(number.ToString("0.00E+0", inv)); // 1.23E+4
int count = 123456789;
Console.WriteLine(count.ToString("0.##E+0", inv)); // 1.23E+8
double small = 0.000123;
Console.WriteLine(small.ToString("0.##E+0", inv)); // 1.23E-4
The standard E specifier
E always prints one digit before the decimal point, then the number of decimals given by the precision (6 if you leave it out), then an exponent with a sign and at least three digits. That is why E2 gives 1.23E+004, not 1.23E+4. The case of the specifier sets the case of the letter: e2 gives 1.23e+004.
Custom patterns
If you need a shorter exponent, use a custom pattern. The part before E describes the mantissa, using the same 0 (always show a digit) and # (show only if significant) placeholders as any custom numeric format. The part after it describes the exponent:
| Pattern | 12345.67890123 | 0.000123 |
|---|---|---|
0.##E+0 | 1.23E+4 | 1.23E-4 |
0.##E+00 | 1.23E+04 | 1.23E-04 |
0.##E0 | 1.23E4 | 1.23E-4 |
0.##E-0 | 1.23E4 | 1.23E-4 |
0.00E+0 | 1.23E+4 | 1.23E-4 |
E+ always writes the sign of the exponent. E and E- write it only when it is negative. The number of zeros after the sign is the minimum number of exponent digits. Use 0.00 in the mantissa instead of 0.## when you want a fixed number of decimals, so that 1500 becomes 1.50E+3 rather than 1.5E+3.
The value is rounded, not truncated: 0.###E0 turns 1.2345678… into 1.235E4.
In string interpolation
The same specifiers work after the colon in an interpolation hole or a composite format string. To keep the culture fixed, create the string with string.Create (.NET 6 and later):
string text = string.Create(CultureInfo.InvariantCulture,
$"Distance: {number:0.##E+0} km");
// Distance: 1.23E+4 km
string.Format(CultureInfo.InvariantCulture, "{0:0.##E+0}", number) does the same on older frameworks.
Culture matters
The exponent letter and digits are the same everywhere, but the decimal separator is not:
var german = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine(number.ToString("E2", german)); // 1,23E+004
If the string is for people, the current culture is usually right. If it goes into a file, a URL, JSON or another program, use InvariantCulture, or the reader on a German or French machine will get a comma it did not expect.
Parsing it back
double.Parse and double.TryParse accept exponents by default, since their default number style is NumberStyles.Float | NumberStyles.AllowThousands:
double value = double.Parse("1.23E+4", CultureInfo.InvariantCulture);
Console.WriteLine(value); // 12300
int.Parse("1E+3") throws a FormatException, because the default style for integers does not include AllowExponent. Pass NumberStyles.Float if you need it, and the result still has to be a whole number that fits in the type.
When .NET switches on its own
You do not always ask for scientific notation to get it. A plain ToString() on a double switches to it for very large or very small values: 1e20.ToString(CultureInfo.InvariantCulture) returns 1E+20 and 0.00001 becomes 1E-05. If that output ends up in a report, format it explicitly with F or N instead. decimal never switches to exponent form on its own.
In your examples, the output shows 2 to 3 significant digits only, where are the rese of the digits? Example, if the value is: 12345.67890123 it is no good to display it as 1.23E+4 – How to display the full number?
Try to change the string format(##).