Appendix Q
Other Format Specifiers

A program uses format specifiers to determine how objects are represented as strings. C# provides two kinds of specifiers: standard format specifiers and custom format specifiers. Standard format specifiers are locale-aware, so the result depends on the computer’s regional settings. If a program uses standard format specifiers and you run the program in a new locale, it automatically produces the formats appropriate for that locale. For that reason, you should always use the standard specifiers whenever possible.

The following sections describe the standard and custom format specifiers.

Standard Numeric Format Specifiers

Standard numeric format specifiers enable you to easily display commonly used numeric formats. The following table lists the standard numeric specifiers.

SpecifierMeaning
C or cCurrency. If a precision specifier follows the C, it indicates the number of digits that should follow the decimal point. On a standard system in the United States, the value –1234.5678 with the specifier C3 produces ($1,234.568).
D or dDecimal. This specifier works only with integer types. It simply displays the number’s digits. If a precision specifier follows the D, it indicates the number of digits the result should have, padding on the left with zeros, if necessary. If the value is negative, the result has a minus sign on the left. The value –1234 with the specifier D6 produces –001234.
E or eScientific notation. The result always has exactly one digit to the left of the decimal point, followed by more digits, an E or e, a plus or minus sign, and at least three digits of exponent (padded on the left with zeros, if necessary). If a precision specifier follows the E, it indicates the number of digits the result should have after the decimal point. The value –1234.5678 with the specifier e2 produces –1.23e+003.
F or fFixed point. The result contains a minus sign if the value is negative, digits, a decimal point, and then more digits. If a precision specifier follows the F, it indicates the number of digits the result should have after the decimal point. The value –1234.5678 with the specifier f3 produces –1234.568.
G or gGeneral. Either scientific or fixed-point notation depending on which is more compact.
N or nNumber. The result has a minus sign if the value is negative, digits with thousands separators, a decimal point, and more digits. If a precision specifier follows the N, it indicates the number of digits the result should have after the decimal point. The value –1234.5678 with the specifier N3 produces –1,234.568.
P or pPercentage. The value is multiplied by 100 and then formatted according to the computer’s settings. If a precision specifier follows the P, it indicates the number of digits that should follow the decimal point. The value 1.2345678 with the specifier P produces 123.46%.
R or rRound trip. The value is formatted in such a way that the result can be converted back into its original value. Depending on the data type and value, this may require 17 digits of precision. The value 1/7 with the specifier R produces 0.14285714285714285.
X or xHexadecimal. This works for integer types only. The value is converted into hexadecimal. The case of the X or x determines whether hexadecimal digits above 9 are written in uppercase or lowercase. If a precision specifier follows the X, it indicates the number of digits the result should have, padding on the left with zeros, if necessary. The value 183 with the specifier x4 produces 00b7.

Custom Numeric Format Specifiers

The following table lists characters that you can use to build custom numeric formats.

SpecifierMeaning
0A digit or zero. If the number doesn’t have a digit in this position, the specifier adds a 0. The value 12 with the specifier 000.00 produces 012.00.
#A digit. If the number doesn’t have a digit in this position, nothing is printed.
,If used between two digits (either 0 or #), this adds thousands separators to the result. Note that it will add as many separators if necessary. The value 1234567 with the specifier #,# produces 1,234,567.
,If used immediately to the left of the decimal point, the number is divided by 1000 for each comma. The value 1234567 with the specifier #,#,. produces 1,235.
%Multiplies the number by 100 and inserts the % symbol where it appears in the specifier. The value 0.123 with the specifier .00% produces 12.30%.
E0 or e0Displays the number in scientific notation inserting an E or e between the number and its exponent. Use # and 0 to format the number before the exponent. The number of 0s after the E determines the number of digits in the exponent. If you place a + sign between the E and 0, the result’s exponent includes a + or – sign. If you omit the + sign, the exponent includes a sign only if it is negative. The value 1234.5678 with the specifier 00.000E+000 produces 12.346E+002.
Displays the following character literally without interpreting it. Use \ to display the character. The value 12 with the specifier #\% produces 12%. The same value with the specifier #% produces 1200%.
'ABC' or "ABC"Displays the characters in the quotes literally. The value 12 with the specifier #'%' produces 12%.

Numeric Formatting Sections

A numeric format specifier may contain one, two, or three sections separated by semicolons. If the specifier contains one section, the specifier is used for all numeric values.

If the specifier contains two sections, the first is used to format values that are positive or zero, and the second is used to format negative values.

If the specifier contains three sections, the first is used to format positive values, the second is used to format negative values, and the third is used to format values that are zero.

The following text shows output from the Immediate window for three values using the format specifier #,#.00;<#,#.00>;ZERO.

(1234.5678).ToString("#,#.00; <#,#.00>;ZERO")
"1,234.57"
(-1234.5678).ToString("#,#.00; <#,#.00>;ZERO")
" <1,234.57>"
(0).ToString("#,#.00; <#,#.00>;ZERO")
"ZERO"

Composite Formatting

The String.Format, Console.WriteLine, and TextWriter.WriteLine methods provide a different method for formatting strings. These routines can take a composite formatting string parameter that contains literal characters plus placeholders for values. Other parameters to the methods give the values.

The value placeholders have the following format.

{index«,alignment»«:format_specifier»}

The index value gives the index of the parameter (numbered from 0) that should be inserted in this placeholder’s position.

The optional alignment value tells the minimum number of characters the item should use and the result is padded with spaces, if necessary. If this value is negative, the result is left-justified. If the value is positive, the result is right-justified.

The format_specifier indicates how the item should be formatted.

For example, consider the following code.

string name = "Crazy Bob";
decimal sales = -12345.67m;
MessageBox.Show(String.Format("{0} {1:earned;lost} {1:c} this year", name, sales));

The first placeholder refers to parameter number 0. The first parameter after the format specifier is the name variable, which has value Crazy Bob.

The second placeholder refers to parameter number 1 and includes a two-part format specifier that displays earned if the value is positive or zero, and lost if the value is negative. The third placeholder refers to parameter number 1 again, this time displaying its value formatted as currency.

The following shows the result.

Crazy Bob lost ($12,345.67) this year

Enumerated Type Formatting

Enumerated values are formatted as string representations rather than as the integer values that represent them. For example, consider the following code.

public enum Dessert
{
    Cake = 1,
    Pie = 2,
    Cookie = 3,
    IceCream = 4,
}
...
Dessert selection = Dessert.Cake;
MessageBox.Show(selection.ToString());

This code displays the string Cake.

For variables of an enumerated type such as selection, the ToString method can take a specifier that determines how the value is formatted.

The specifier G or g formats the value as a string if possible. If the value is not a valid entry in the enumeration’s definition, the result is the variable’s numeric value. For example, the previous code does not define a value for the integer 7 so, if you set selection to 7, selection.ToString("G") returns the value 7.

If you define an enumerated type with the Flags attribute, variables of that type can be a combination of the enumeration’s values, as shown in the following code.

[Flags()]
public enum Dessert
{
    Cake = 1,
    Pie = 2,
    Cookie = 3,
    IceCream = 4,
}
...
Dessert selection = Dessert.IceCream | Dessert.Pie;
Console.WriteLine(selection.ToString("G"));

In this case, the G format specifier returns a string that contains all the flag values separated by commas. In this example, the result is Pie, IceCream. Note that the values are returned in the order in which they are defined by the enumeration, not the order in which they are assigned to the variable.

If you do not use the Flags attribute when defining an enumerated type, the G format specifier always returns the variable’s numeric value if it is a combination of values rather than a single value from the list. In contrast the F specifier returns a list of comma-separated values if it makes sense. For example, if you omit the Flags attribute from the previous code, selection.ToString("G") returns 6 (because Pie + IceCream = 2 + 4 = 6), but selection.ToString("F") returns Pie, IceCream.

The D or d specifier always formats the variable as a number.

The specifier X or x formats the value as a hexadecimal number.

The following table summarizes the enumeration format specifiers.

SpecifierMeaning
G or g(The default.) Returns the string value if possible.
If the value includes multiple enumeration values and the enumeration is marked with the Flags attribute, this returns the values separated by commas.
If the value is not defined by the enumeration, or if it includes multiple enumeration values but the enumeration does not have the Flags attribute, this returns a numeric value.
F or fReturns the string value if possible.
If the value includes multiple enumeration values, this returns the values separated by commas.
If the value is not defined by the enumeration, this returns a numeric value.
D or dAlways returns a numeric result.
X or xAlways returns a hexadecimal numeric result.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.255.36