C#

C# Numeric Formats

The objective of this article is to familiarize you with the C# Numeric Formats. You will learn different options available for C# numeric formatting. If standard formatting doesn’t work for you then use custom formatting to add custom text to the output string.

Standard C# Numeric Formats

The following table shows the most common numeric formatting options in C#.

Code Name Description
C or c Currency Display a number that represents a currency amount.
D or d Digits Display integer with the specified number of digits.
E or e Exponential Display a number in exponential notation.
F or f Float Display number as a decimal with the fixed number of decimal places.
N or n Number Display numerical formatting with commas and the specified number of decimal places.
P or p Percent Display percentage with the specified number of decimal places.
X or x Hexadecimal Display Hexadecimal formatted value.
2AF3 hex = 10,995 decimal (integral types only).

The following example uses string interpolation to create a string and Console.WriteLine() method would write the same string to the console. A string with $ sign can use placeholder {…} that takes the number variable and output the value assigned to it at that position in the string.

int number = 7861;
Console.WriteLine($"Number: {number}"); // Number: 7861

You can use any valid C# expression within the placeholder. Let’s change the output by including the format specifier within the placeholder. The specifier indicates whether or not values should be converted into a currency format, percentage, etc. This can be achieved by appending the expression with a colon (:) and then specifier. For example, {number:C} output the value of number variable as a currency amount, $78,612.00.

int number = 7861;

Console.WriteLine($"C -> {number:C}"); // $7,861.00
// Precision specifier indicates the desired number of decimal places.
Console.WriteLine($"C0 -> {number:C0}"); // $7,861
Console.WriteLine($"C1 -> {number:C1}"); // $7,861.0
Console.WriteLine($"C2 -> {number:C2}"); // $7,861.00
Console.WriteLine($"C3 -> {number:C3}"); // $7,861.000
Console.WriteLine($"D -> {number:D}"); // 7861
Console.WriteLine($"D3 -> {number:D3}"); // 7861
Console.WriteLine($"D4 -> {number:D4}"); // 7861
Console.WriteLine($"D5 -> {number:D5}"); // 07861
Console.WriteLine($"D6 -> {number:D6}"); // 007861

Replaces the zero with the number if one is present at that position; otherwise, zero appears in the result string to produce the number of digits given by the precision specifier.

Console.WriteLine($"E -> {number:E}"); // 7.861000E+003
Console.WriteLine($"e -> {number:e}"); // 7.861000E+003

Console.WriteLine($"F -> {number:F}"); // 7861.00
// Precision specifier indicates the desired number of decimal places.
Console.WriteLine($"F0 -> {number:F0}"); // 7861
Console.WriteLine($"F1 -> {number:F1}"); // 7861.0
Console.WriteLine($"F2 -> {number:F2}"); // 7861.00

Console.WriteLine($"N -> {number:N}"); // 7,861.00
// Precision specifier indicates the desired number of decimal places.
Console.WriteLine($"N0 -> {number:N0}"); // 7,861
Console.WriteLine($"N1 -> {number:N1}"); // 7,861.0
Console.WriteLine($"N2 -> {number:N2}"); // 7,861.00

Console.WriteLine($"P -> {1:P}"); // 100.00%
Console.WriteLine($"P0 -> {1:P0}"); // 100%

Console.WriteLine($"X -> {10995:X}"); // 2AF3

Run Demo

Custom C# Numeric Formats

If standard formatting doesn’t meet your requirements than use custom formatting to add custom text to the output string. These specifiers let you manually create new formatting patterns to suit your needs.

The following table lists the most frequently used custom numeric format specifiers in C#.

Code Name Description
0 Zero placeholder Display digit if present; otherwise, 0 appears in the output string.
# Digit placeholder Display digit if present; otherwise, nothing appears in the output string.
. Decimal point Display the number with a decimal separator and a fixed number of decimal places.
, Thousands separator Display the number with thousands separator.
% Percentage placeholder Multiplies a number by 100 and adds a % symbol.
; Section separator Specifies three sections with separate format strings for values > 0, < 0, = 0

Zero Placeholder (0)

Console.WriteLine($"1 -> {786.1:000}"); // 786
Console.WriteLine($"2 -> {786:0000}"); // 0786
Console.WriteLine($"3 -> {786:00000}"); // 00786
Console.WriteLine($"4 -> {786:000000}"); // 000786

Replaces zero with the number if one is present at that position; otherwise, zero appears in the result string to produce the number of digits required by the custom specifier.

Formatted with single decimal place.
Console.WriteLine($"2 -> {786.1:000.0}"); // 786.1

// Display zero, if the number includes fewer decimal places then the custom specifier.
Console.WriteLine($"3 -> {786.1:000.00}"); // 786.10

// Number is rounded if includes more decimal places then the custom specifier.
Console.WriteLine($"4 -> {786.128:000.00}"); // 786.13

Digit Placeholder (#)

Console.WriteLine($"1 -> {78.7:0.#}"); // 78.7
Console.WriteLine($"2 -> {1.7:#0.0}"); // 1.7

Console.WriteLine($"3 -> {2.7:0#.#}"); // 02.7
Console.WriteLine($"4 -> {2.7:0##.#}"); // 002.7

// Number is rounded if includes more decimal places then the custom specifier.
Console.WriteLine($"5 -> {786.36:0.#}"); // 786.4
Console.WriteLine($"6 -> {786.348:0.##}"); // 786.35
Console.WriteLine($"7 -> {786.8:0##}"); // 787

Thousands Separator ( , )

Console.WriteLine($"1 -> {78612.3:#,#.#}"); // 78,612.3
Console.WriteLine($"2 -> {78612.3:#,#}"); // 78,612

Percentage Placeholder ( % )

// Multiplied by 100 before it is formatted
 Console.WriteLine($"1 -> {.7861:#00.##%}"); // 78.61%

Section Separator ( ; )

The following example shows how you can use different formats to positive, negative and zero values. To achieve this, section separator is used to split format string into three sections delimited by semicolons (;). The first part is used to format positive numbers, the second is used for negative numbers and the third is for zero. If you leave out the last part then the first part will be used for zero.

If you want the output string to have a minus and plus sign then you need to explicitly add these signs as part of the custom format specifier.

Console.WriteLine($"1 -> {7.71:+0.00;-0.00;0}"); // +7.71
Console.WriteLine($"2 -> {-8.81:+0.00;-0.00;0}"); // -8.81
Console.WriteLine($"3 -> {0.0:+0.00;-0.00;0}"); // 0
        
Console.WriteLine($"4 -> {7:+#;-#;0}"); // +7
Console.WriteLine($"5 -> {-8:+#;-#;0}"); // -8
Console.WriteLine($"6 -> {0:+#;-#;0}"); // 0

Run Demo

C# Reference | Microsoft Docs

Sending
User Rating 0 (0 vote)
Tagged in :

Send this to a friend