Appendix P : 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.

Open table as spreadsheet

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.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net