WriteLine formatting in C#
WriteLine is used to output a line of string in C#. It is often required to output a integer, string or other variable in a certain way. We need to use formatting in such cases.
The format parameter in formatting is embedded with zero or more format specifications of the form "{ N [, M ][: formatString ]}", arg1, ... argN, where:
* N is a zero-based integer indicating the argument to be formatted.
* M is an optional integer indicating the width of the region to contain the formatted value, padded with spaces. If M is negative, the formatted value is left-justified; if M is positive, the value is right-justified.
* formatString is an optional string of formatting codes.
* argN is the expression to use at the equivalent position inside the quotes in the string.
Consider the following example to understand it
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.WriteLine("{0,5} {1,5}", 123, 456); // Right-aligned Console.WriteLine("{0,-5} {1,-5}", 123, 456); // Left-aligned Console.WriteLine("{0,-10:D6} {1,-10:D6}", 123, 456); // D6 means 6 decimal digits Console.ReadLine(); } } } |
123 456 123 456 000123 000456 |
Format Specifiers
Standard numeric format strings are used to return strings in commonly used formats. They take the form X0, in which X is the format specifier and 0 is the precision specifier. The format specifier can be one of the nine built-in format characters that define the most commonly used numeric format types, as shown in Table 10-1.
Table 10-1 - String and WriteLine Format Specifiers
Character |
Interpretation |
C or c |
Currency |
D or d |
Decimal (decimal integer—don’t confuse with the .NET |
E or e |
Exponent |
F or f |
Fixed point |
G or g |
General |
N or n |
Currency |
P or p |
Percentage |
R or r |
Round-trip (for floating-point values only); guarantees that a numeric value converted to a string will be parsed back into the same numeric value |
X or x |
Hex |
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int i = 123456; Console.WriteLine("{0:C}", i); // $123,456.00 Console.WriteLine("{0:D}", i); // 123456 Console.WriteLine("{0:E}", i); // 1.234560E+005 Console.WriteLine("{0:F}", i); // 123456.00 Console.WriteLine("{0:G}", i); // 123456 Console.WriteLine("{0:N}", i); // 123,456.00 Console.WriteLine("{0:P}", i); // 12,345,600.00 % Console.WriteLine("{0:X}", i); // 1E240 Console.ReadLine(); } } } |
If you run above you should see the following command
$123,456.00 123456 1.234560E+005 123456.00 123456 123,456.00 12,345,600.00 % 1E240 |
If the standard formatting specifiers aren’t enough for you, you can use picture format strings to create custom string output. Picture format definitions are described using placeholder strings that identify the minimum and maximum number of digits used, the placement or appearance of the negative sign, and the appearance of any other text within the number, as shown in Table 10-2.
Table 10-2 - Custom Format Specifiers
Format Character |
Purpose |
Description |
0 |
Display zero placeholder |
Results in a nonsignificant zero if a number has fewer digits than there are zeros in the format |
# |
Display digit placeholder |
Replaces the pound symbol (#) with only significant digits |
. |
Decimal point |
Displays a period (.) |
, |
Group separator |
Separates number groups, as in 1,000 |
% |
Percent notation |
Displays a percent sign (%) |
E+0 |
Exponent notation |
Formats the output of exponent notation |
\ |
Literal character |
Used with traditional formatting sequences such as “\n” (newline) |
'ABC' |
Literal string |
Displays any string within quotes or apostrophes literally |
; |
Section separator |
Specifies different output if the numeric value to be formatted is positive, negative, or zero |
Let’s see the strings that result from a set of customized formats, using first a positive integer, then using the negative value of that same integer, and finally using zero:
The example below shows the results in comments based upon the format specifier.
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int i = 123456; Console.WriteLine(); Console.WriteLine("{0:#0}", i); // 123456 Console.WriteLine("{0:#0;(#0)}", i); // 123456 Console.WriteLine("{0:#0;(#0); |
This completes your understanding of formatting in C#. You like to experiment on your own to further strengthen your understanding of WriteLine formatting. We will use WriteLine formatting to understand some aspects of C# in next few chapters.