Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

C# format specifiers

Reference for C# format specifiers: standard numeric (F, N, C, P, X, E), custom numeric patterns, date and time formats, and enum and TimeSpan formats.

A format specifier is a short string that tells ToString, string.Format, interpolation and Console.WriteLine how to turn a value into text. In x.ToString("F2") it is F2; in $"{x:F2}" it is the part after the colon.

There are three groups: standard formats (one letter plus an optional precision, such as N1), custom formats (a pattern of placeholder characters, such as 000.00), and the equivalents for dates, times, time spans, enums and GUIDs. Culture decides separators and symbols. This site's runner uses the invariant culture, so the output below is what CultureInfo.InvariantCulture produces everywhere.

C#
(1234.5).ToString("N2");           // "1,234.50"
new DateTime(2026, 3, 9).ToString("yyyy-MM-dd");   // "2026-03-09"

Try it

C# Standard numeric formats

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 numeric, date, time span and enum formats

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

Standard numeric format: one letter and an optional precision, for example F2.

Specifier Meaning
F Fixed point: 1234.57
N Number with group separators: 1,234.57
C Currency, using the culture's symbol
P Percent: multiplies by 100 and adds a percent sign
E Scientific notation: 1.235E+003
G General: the shorter of fixed or scientific
D Decimal digits, integers only; precision is minimum digits
X, x Hexadecimal, integers only
B Binary, integers only (.NET 8 and later)
R Round-trip, for floating-point values

Custom numeric characters: 0 (digit or zero), # (digit or nothing), . (decimal point), , (group separator or scaling), % (percent), e/E (exponent), ; (separates positive, negative and zero sections), and 'text' or \ for literals.

Date and time: yyyy year, MM month number, MMM/MMMM month name, dd day, ddd/dddd day name, HH 24-hour, hh 12-hour, mm minute, ss second, fff milliseconds, tt AM/PM, zzz offset. Standard single letters include d, D, t, T, f, F, o/O and s.

Errors / undefined behavior: an invalid specifier for the type throws FormatException (for example "D" on a double). Format strings given to types that ignore them (such as string) are ignored.

Usage notes

  • Precision means different things. For F, N, E and P it is digits after the decimal point; for G it is significant digits; for D and X it is the minimum number of digits.
  • Rounding. F and N round away from zero at the midpoint of the decimal representation. Binary floating-point values such as 2.675 may not be exactly at the midpoint, so the result can look wrong. Use decimal for money.
  • Single-character custom formats are read as standard formats. To use a custom pattern consisting of one character, prefix it with %, for example "%d" for the day of the month.
  • Colons and slashes in dates are culture placeholders. To print a literal one, escape it (\:) or quote it. In TimeSpan formats you must escape every literal character.
  • Month versus minute: MM is month, mm is minutes. Mixing them up is the most common date formatting bug.
  • Percent in the invariant culture is written with a space before the sign (25.6 %); other cultures differ.
  • Culture. Always pass CultureInfo.InvariantCulture for machine-readable output such as files and APIs.
  • Currency in invariant culture uses the generic currency sign. Real symbols need a specific culture, which is read-only knowledge on this site.
  • Interpolated strings take the same specifiers after a colon: $"{price:F2}". An expression that itself contains a colon, such as a conditional a ? b : c, needs parentheses.