Appendix Q

Other Format Specifiers

A program uses format specifiers to determine how objects are represented as strings. For example, by using different format specifiers, you can make an integer’s ToString method return a value as –12345, –12,345, (12,345), or 012,345–.

Visual Basic provides standard format specifiers in addition to custom specifiers. The standard specifiers make it easy to display values in often-used formats (such as currency or scientific notation). Custom specifiers provide more control over how results are composed.

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.

SPECIFIER MEANING
C or c Currency. The exact format depends on the computer’s internationalization settings. 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 C produces ($1,234.57).
D or d Decimal. 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 e Scientific 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 f Fixed 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 g General. Either scientific or fixed-point notation depending on which is more compact.
N or n Number. 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 p Percentage. 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. On a typical computer, the value 1.2345678 with the specifier P produces 123.46%.
R or r Round 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 x Hexadecimal. 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

Custom numeric format specifiers describe how a number should be formatted. The following table lists characters that you can use to build custom numeric formats.

SPECIFIER MEANING
0 A 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 #), adds thousands separators to the result. Note that it will add many comma 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 e0 Displays 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 only includes a sign 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%, and the same value with the specifier #% produces 1200%.
‘ABC’ or “ABC” Displays the characters in the quotes literally. The value 12 with the specifier #‘%’ (single quotes around the % symbol) 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 numbered from 0 of the parameter that should be inserted in this placeholder’s position.

The optional alignment value tells the minimum number of spaces 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:

Dim emp As String = "Crazy Bob":
Dim sales As Single = -12345.67
MessageBox.Show(String.Format("{0} {1:earned;lost} {1:c} this year", emp, sales))

The first placeholder refers to parameter number 0, which has the 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” of the value is negative. The third placeholder refers to parameter number 1 again, this time formatted as currency.

The following code shows the result:

Crazy Bob lost ($12,345.67) this year

ENUMERATED TYPE FORMATTING

Visual Basic provides special formatting capabilities that can display the values of enumerated variables. For example, consider the following code:

Private Enum Dessert
    Cake = 1
    Pie = 2
    Cookie = 3
    IceCream = 4
End Enum
...
Dim dessert_choice As Dessert = Dessert.Cake
MessageBox.Show(dessert_choice.ToString)

This code displays the string “Cake.”

For variables of an enumerated type such as dessert_choice, 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 Enum’s definition, the result is the variable’s numeric value. For example, the previous code does not define a Dessert enumeration for the value 7 so, if you set dessert_choice to 7, then dessert_choice.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 Enum’s values, as shown in the following code:

<Flags()>
Private Enum Dessert
    Cake = 1
    Pie = 2
    Cookie = 4
    IceCream = 8
End Enum
...
Dim dessert_choice As Dessert = Dessert.IceCream Or Dessert.Cake
MessageBox.Show(dessert_choice.ToString("G"))

In this case, the G format specifier returns a string that contains all of the flag values separated by commas. In this example, the result is “Cake, 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. If you omit the Flags attribute from the previous code, dessert_choice.ToString(“G”) returns 9, but dessert_choice.ToString(“F”) returns “Cake, 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.

..................Content has been hidden....................

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