Information Technology Reference
In-Depth Information
The Alignment Specifier
The alignment specifier represents the minimum width of the field in terms of characters. The
alignment specifier has the following characteristics:
￿
It is optional and separated from the index with a comma.
￿
It consists of a positive or negative integer.
-
The integer represents the minimum number of characters to use for the field.
-
The sign represents either right or left alignment. Positive specifies right alignment;
negative specifies left alignment.
Index -- use 0th item in the list.
Console.WriteLine("{0, 10 }", 500);
Alignment specifier -- right-align in a field of 10 characters.
For example, the following code shows two format items, formatting the value of int
variable MyInt . In the first case, the value of MyInt is displayed as a right-aligned string of ten
characters. In the second case, it is left aligned. The format items are between two vertical bars,
just to show in the output their limits on each side.
int MyInt = 500;
Console.WriteLine("|{0, 10}|", MyInt); // Aligned right
Console.WriteLine("|{0,-10}|", MyInt); // Aligned left
This code produces the following output; there are ten characters between the vertical bars:
| 500|
|500 |
The actual representation of the value might take more or fewer characters than specified
in the alignment specifier:
￿
If the representation takes fewer characters than is specified in the alignment specifier,
the remaining characters are padded with spaces.
￿
If the representation takes more characters than specified, the alignment specifier is
ignored, and the representation uses as many characters as is needed.
Search WWH ::




Custom Search