jrliv
5/15/2017 - 3:37 AM

Formatted output in C#.

Formatted output in C#.

DateTime d = DateTime.Now;
DateTime d2 = new DateTime(2008, 08, 21, 10, 30, 14);

Console.WriteLine("Using DateTime.Now will print the current date: " + d + "\n");
// Using DateTime.Now will print the current date: *current date and time*

// Examples using defined date formats.
Console.WriteLine("{0:d}", d2);		// 8/21/2008
Console.WriteLine("{0:D}", d2);		// Thursday, August 21, 2008
Console.WriteLine("{0:t}", d2);		// 10:30 AM
Console.WriteLine("{0:T}", d2);		// 10:30:14 AM
Console.WriteLine("{0:y}\n", d2);	// August 2008

// Examples using custom date formats.
Console.WriteLine("{0:dd/MM/yyyy HH:mm:ss}", d2);	// 21/08/2008 10:30:14
Console.WriteLine("{0:d.MM.yy}", d2);				// 21.08.08
Console.WriteLine("{0:M/d/yyyy}", d2);				// 8/21/2008
Console.WriteLine("{0:dd/MM/yyyy HH:mm:ss}", d2);	// 21/08/2008 10:30:14
Console.WriteLine("{0:d-MM-yy}", d2);				// 21-08-08
Console.WriteLine("{0:M d yyyy}", d2);				// 8 21 2008
Console.WriteLine("{0:ddd, MMM d, yyyy}", d2); 		// Thu, Aug 21, 2008
Console.WriteLine("{0:dddd, MMMM d, yyyy}", d2);  	// Thursday, August 21, 200
//Normal string.
Console.WriteLine("This is a normal string.");
//Output: This is a normal string.

//Formatted string 
Console.WriteLine("This is a formatted {0}", "string");
//Output: This is a formatted string
Console.WriteLine("This is a number: {0}, that is used in a formatted string.", 25);
//Output: This is a number: 25 that is used in a formatted string.

//Formatted string using variable.
string str = "formatted string using a string variable.";
Console.WriteLine("This is a {0}", str);
//Output: This is a formatted string using a string variable.

//General syntax for formatting strings: {index[,alignment][:formatString]}
//Another example using the index component.
Console.WriteLine("{0} is {1} years old {3}.\n", "Bob", 20, "already", "today");
//Output: Bob is 20 years old today.

//Example of using the optional [,alignment] component.
//It can "shift" a string by inserting a desired number of spaces.
//A positive value indicates the alignment will shift to the right.
//A negative value indicates the alignment will shift to the left.
//If the string represented has a length greater than or equal to the value of the number, it is ignored. 
//If it is less, the unfilled positions are filled in with spaces.
Console.WriteLine("abc");
//Output: abc
Console.WriteLine("{0,6}", 123);
//Output:    123
Console.WriteLine("{0,6}", 12);
//Output:     12
Console.Write("{0,-6}", "test");
Console.Write("{0,-8}", 321);
Console.WriteLine("this is the end\n");
//Output: test  321     this is the end

//Example of using optional [:formatString] component.
//This component sets the specific formatting of the string. Its use depends on the type of argument.
//There are standard formats and user defined formats.

//Examples of standard formats for numbers.
Console.WriteLine("Currency example: {0:C2}", 1200.501453);
//Output: Currency example: $1,200.50

Console.WriteLine("Decimal example: {0:D6}", -123);
//Output: Decimal example: -000123
Console.WriteLine("Decimal example: {0:D4}", 321);
//Output: Decimal example: 0321

Console.WriteLine("Exponential example: {0:E4}", 123);
//Output: Exponential example: 1.2300E+002
Console.WriteLine("Exponential example: {0:E1}", 321);
//Output: Exponential example: 3.2E+002

Console.WriteLine("Fixed-point example: {0:F2}", -123.456);
//Output: Fixed-point example: -123.46
Console.WriteLine("Fixed-point example: {0:F4}", 987.654321);
//Output: Fixed-point example: 987.6543
Console.WriteLine("Fixed-point example: {0:F4}", 987.65);
//Output: Fixed-point example: 987.6500

Console.WriteLine("Number example: {0:N2}", 1234567.8960);
//Output: Number example: 1,234,567.90
Console.WriteLine("Number example: {0:N2}", 1000);
//Output: Number example: 1,000.00
Console.WriteLine("Number example: {0:N4}", 100000);
//Output: Number example: 100,000.0000

Console.WriteLine("Percent example: {0:P4}", 0.456789);
//Output: Percent example: 45.6789 %
Console.WriteLine("Percent example: {0:P}", 0.456789);
//Output: Percent example: 45.68 %
Console.WriteLine("Percent example: {0:P}", 2.50);
//Output: Percent example: 250.00 %
Console.WriteLine("Percent example: {0:P4}", 0.1234);
//Output: Percent example: 12.3400 %

Console.WriteLine("Hexadecimal example: {0:X}", 43);
//Output: Hexadecimal example: 2B
Console.WriteLine("Hexadecimal example: {0:x}", 43);
//Output: Hexadecimal example: 2b
Console.WriteLine("Hexadecimal example: {0:X4}", 43);
//Output: Hexadecimal example: 002B

//Examples of using custom formats.
//Custom formats also use a set of specifiers as shown below.
Console.WriteLine("Zero placeholder: {0:00000}", 321);
//Output: Zero placeholder: 00321
Console.WriteLine("Zero placeholder: {0:0.00}", 1.2);
//Output: Zero placeholder: 1.20
Console.WriteLine("Digit placeholder and decimal point: {0:#.##}", 0.2345);
//Output: Digit placeholder and decimal point: .23
Console.WriteLine("Digit placeholder and decimal point: {0:#.##}", 4.3);
//Output: Digit placeholder and decimal point: 4.3
Console.WriteLine("Digit placeholder: {0:####}", 1234.567);
//Output: Digit placeholder: 1235
Console.WriteLine("Digit placeholder: {0:(0#) ### ## ##}", 123456789);
//Output: (12) 345 67 89
Console.WriteLine("Digit and percentage placeholder: {0:%##}", 0.234);
//Output: Digit and percentage placeholder: %23