Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Numbers and Math

C# number ToString() and format strings

Reference for formatting numbers in C# with ToString(): standard formats (N, F, C, P, X, E, G, R), custom patterns, precision and culture rules.

Every numeric type has a ToString method that turns the number into text. Without arguments it gives the shortest round-trippable form. With a format string, you control digits, grouping, padding, percent signs, hexadecimal and more. The same format strings work in interpolated strings ($"{price:F2}"), in string.Format, and in Console.WriteLine.

There are two kinds of format string. A standard format is a single letter with an optional precision, such as F2 or X4. A custom format is a pattern built from placeholders, such as 0.00 or #,##0.0. Currency symbols, separators and the percent sign come from the culture, which is why you should pass CultureInfo.InvariantCulture for stored data.

C#
double price = 1234.5;
Console.WriteLine(price.ToString("F2"));     // 1234.50
Console.WriteLine($"{price:N1}");           // 1,234.5

Try it

C# Standard format strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Custom patterns and interpolation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public string ToString()
public string ToString(string? format)
public string ToString(IFormatProvider? provider)
public string ToString(string? format, IFormatProvider? provider)
public bool   TryFormat(Span<char> destination, out int charsWritten,
                        ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
Specifier Meaning
F / F2 Fixed-point. The number is the count of decimals (default 2).
N / N2 Like F, with culture group separators.
D / D6 Integer types only: digits, padded with zeros to the precision.
E / E3 Exponential (scientific) notation.
G / G4 General: shorter of fixed or scientific. The number is the count of significant digits.
R Round-trip. Equivalent to the default in .NET Core 3.0 and later.
C Currency, using the culture's symbol and layout.
P Percent: multiplies by 100 and adds the percent symbol.
X / X4 Hexadecimal for integers, with padding. x gives lowercase.
B / B8 Binary for integers (.NET 8+).
Custom 0 digit placeholder, # optional digit, . decimal point, , group separator or scaling, % percent, e+0 exponent, ; section separator, 'text' literal.

Returns: the formatted string. TryFormat writes into a span and returns true when it fits, which avoids allocations.

Errors: a malformed standard format throws FormatException (for example 42.ToString("Q")). Unrecognized characters in a custom pattern are copied as literals.

Usage notes

  • The default is short. double.ToString() gives the shortest string that parses back to the same value, so 0.1 + 0.2 shows 0.30000000000000004. Ask for F20 to see more of the underlying binary value.
  • Rounding. Fixed-point formats round the exact binary value, so 1.005.ToString("F2") gives 1.00 because 1.005 is stored slightly below 1.005. Decimal types (decimal) round from their decimal digits instead. The runner printed 2 for 2.5.ToString("F0") and 4 for 3.5.ToString("F0"), so do not rely on the direction of exact ties; when it matters, round first with Math.Round(x, MidpointRounding.AwayFromZero).
  • Always set the culture for data. Without a provider, the current culture decides the decimal point and group separator. This runner always uses the invariant culture, so culture-specific output (such as de-DE) is not shown here.
  • Invariant layout. With the invariant culture, P puts a space before the sign (25.6 %), E3 writes a three-digit exponent (1.235E+006) and C uses the generic currency sign ยค.
  • Currency. The invariant culture uses the generic currency sign. For real money formatting, pick a specific CultureInfo or format the symbol yourself.
  • Hex of negatives. (-1).ToString("X") is FFFFFFFF: the two's-complement bit pattern of the type.
  • Allocation-free. Use TryFormat or Utf8Formatter-style APIs (IUtf8SpanFormattable) in hot paths.
  • Interpolated strings accept the same format after a colon and an alignment width before it: {value,10:F2}.
  • Custom , has two meanings: between digit placeholders it adds group separators; right before the decimal point or at the end it scales by 1000 per comma.