Description |
Writes the specified data, with no line terminator, to the standard output stream.
Format provides a very rich and well organised set of formatting options for the integer, decimal, datetime and enumeration data types.
The data parameters to the Format syntax may be passed as a discrete set of up to 3 variables, or as an unlimited array of objects.
The formatting string comprises 0, 1 or more sections of plain text, interspersed with formatting string sections. These formatting sections are delimited by {} brackets.
You may use { and } in the plain text by repeating each : {{ and }} respectively.
The formatting sections each have the following general syntax :
{Index[,Width][:Formatting]}
Where :
Index | Is the parameter number (0 indexed) |
Width | Defines the whole field width |
Formatting | Defines a specific format to use |
The formatting value has the following syntax :
FormattingChar[Precision]
Unlike the non-.Net Delphi Format Run Time Library function, the .Net formatting character set is split into 3 groups, applicable to the general data type. For example, the D formatting character applied to a number yields a Decimal string, but applied to a DateTime will yield a LongDate string.
The optional Precision forces the number of decimal places or significant digits as appropriate.
1. Number formatting characters
C or c | Currency such as ?1,234.56 |
D or d | Integer formatting such as 123456 |
E or e | Exponential (Scientific) such as 1.23456E+003 |
F or f | Fixed Point such as 1234.56 |
G or g | General : type dependent |
N or n | Number : -d,ddd,ddd.ddd... format |
P or p | Percent such as 69.5% |
R or r | Round-trip - guarantees reverse formatting |
X or x | Hex such as E06FC (using X) or e06fc (using x) |
2. DateTime formatting characters
d | Short Date, such as : 24/08/2004 |
D | Long date, such as 24 August 2004 |
t | Short time, such as 12:23 |
T | Long time, such as 12:23:45 |
f | Full datetime, such as 24 August 2004 12:23 |
F | Full datetime, such as 24 August 2004 12:23:45 |
g | General datetime, such as 24/08/2004 12:23 |
G | General datetime, such as 24/08/2004 12:23:45 |
M or m | Month, such as 24 August |
R or r | Culture independant, such as Tue 24 August 2004 12:23:45 GMT |
s | Sortable datetime, such as 2004-08-24T12:23:45 |
u | Universal sortable datetime |
Y or y | Year plus month, such as August 2004 |
3. Enumeration formatting characters
D | Decimal |
F | Fixed |
G | General |
X | Hex |
|
|
Notes |
The UInt32 and UInt64 versions of the method are not CLS (Common Language Specification) compliant.
Static methods are not methods of an object - they are simply class functions or procedures available at any time.
|
|
Microsoft MSDN Links |
System
System.Console
System.Console.Write
|
|
|
|
A simple example |
program Project1;
{$APPTYPE CONSOLE}
var
bool : Boolean;
letter : Char;
number : Single;
begin
bool := true;
letter := 'Z';
number := 123;
Console.Write('Bool = ');
Console.Write(bool);
Console.Write(' Letter = ');
Console.Write(letter);
Console.Write(' Number = ');
Console.Write(number);
Console.WriteLine;
Console.ReadLine;
end.
|
Bool = True Letter = Z Number = 123
|
|
Illustrating the formatting character options |
program Project1;
{$APPTYPE CONSOLE}
var
decNumber : Decimal;
intNumber : Integer;
floatNum : Single;
when : DateTime;
enum : system.DayOfWeek;
begin
decNumber := 123456.789;
intNumber := 123456789;
floatNum := 0.25;
when := DateTime.Now;
enum := DayOfWeek.Tuesday;
Console.WriteLine('Number formatting examples :');
Console.WriteLine;
Console.Write('C : Currency : {0:C}',
decNumber); Console.WriteLine;
Console.Write('D : Integer (D) : {0:D}',
System.Object (intNumber)); Console.WriteLine;
Console.Write('E : Exponential : {0:E}',
decNumber); Console.WriteLine;
Console.Write('F : Fixed : {0:F}',
decNumber); Console.WriteLine;
Console.Write('G : General : {0:G}',
decNumber); Console.WriteLine;
Console.Write('N : Number : {0:N}',
decNumber); Console.WriteLine;
Console.Write('P : Percent : {0:P}',
System.Object (floatNum)); Console.WriteLine;
Console.Write('R : Round-trip : {0:R}',
System.Object (floatNum)); Console.WriteLine;
Console.Write('X : Hexadecimal (X): {0:X}',
System.Object (intNumber)); Console.WriteLine;
Console.Write('x : Hexadecimal (x): {0:x}',
System.Object (intNumber)); Console.WriteLine;
Console.WriteLine;
Console.WriteLine('DateTime formatting examples :');
Console.WriteLine;
Console.Write('d : Short Date : {0:d}', when); Console.WriteLine;
Console.Write('D : Long Date : {0:D}', when); Console.WriteLine;
Console.Write('t : Short Time : {0:t}', when); Console.WriteLine;
Console.Write('T : Long Time : {0:T}', when); Console.WriteLine;
Console.Write('f : Full date : {0:f}', when); Console.WriteLine;
Console.Write('F : Long full date : {0:F}', when); Console.WriteLine;
Console.Write('g : General : {0:g}', when); Console.WriteLine;
Console.Write('G : Long general : {0:G}', when); Console.WriteLine;
Console.Write('M : Month : {0:M}', when); Console.WriteLine;
Console.Write('R : RFC1123 : {0:R}', when); Console.WriteLine;
Console.Write('s : Sortable : {0:s}', when); Console.WriteLine;
Console.Write('u : Universal sort : {0:u}', when); Console.WriteLine;
Console.Write('Y : Year : {0:Y}', when); Console.WriteLine;
Console.WriteLine;
Console.WriteLine('Enumeration formatting examples :');
Console.WriteLine;
Console.Write('G : General : {0:G}', enum); Console.WriteLine;
Console.Write('F : Fixed : {0:F}', enum); Console.WriteLine;
Console.Write('D : Decimal : {0:D}', enum); Console.WriteLine;
Console.Write('X : Hexadecimal (X): {0:X}', enum); Console.WriteLine;
Console.Write('x : Hexadecimal (x): {0:x}', enum); Console.WriteLine;
Console.ReadLine;
end.
|
Number formatting examples :
C : Currency : ?123,456.79
D : Integer (D) : 123456789
E : Exponential : 1.234568E+005
F : Fixed : 123456.79
G : General : 123456.789
N : Number : 123,456.79
P : Percent : 25.00 %
R : Round-trip : 0.25
X : Hexadecimal (X): 75BCD15
x : Hexadecimal (x): 75bcd15
DateTime formatting examples :
d : Short Date : 02/09/2004
D : Long Date : 02 September 2004
t : Short Time : 11:14
T : Long Time : 11:14:59
f : Full date : 02 September 2004 11:14
F : Long full date : 02 September 2004 11:14:59
g : General : 02/09/2004 11:14
G : Long general : 02/09/2004 11:14:59
M : Month : 02 September
R : RFC1123 : Thu, 02 Sep 2004 11:14:59 GMT
s : Sortable : 2004-09-02T11:14:59
u : Universal sort : 2004-09-02 11:14:59Z
Y : Year : September 2004
Enumeration formatting examples :
G : General : Tuesday
F : Fixed : Tuesday
D : Decimal : 2
X : Hexadecimal (X): 00000002
x : Hexadecimal (x): 00000002
|
|