Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
20
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 25 OTHER TOPICS

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 ten 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’s 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 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 are needed.

668

CHAPTER 25 OTHER TOPICS

The Format Component

The format component specifies the form that the numeric representation should take. For example, should it be represented as currency, in decimal format, in hexadecimal format, or in fixed-point notation?

The format component has two parts, as shown in Figure 25-2:

The format specifier is a single alphabetic character, from a set of nine built-in character formats. The character can be uppercase or lowercase. The case is significant for some specifiers but not for others.

The precision specifier is optional and consists of one or two digits. Its actual meaning depends on the format specifier.

Figure 25-2. Standard format specifier string

The following code shows an example of the syntax of the format string component:

Index—use 0th item in the list

Console.WriteLine("{0: F4}", 12.345678);

 

Format component—fixed-point, four decimal places

 

 

The following code shows examples of different format strings:

 

 

 

 

 

double myDouble = 12.345678;

 

 

Console.WriteLine("{0,-10:G} -- General",

myDouble);

Console.WriteLine("{0,-10} -- Default, same as General",

myDouble);

Console.WriteLine("{0,-10:F4} -- Fixed Point, 4 dec places",

myDouble);

Console.WriteLine("{0,-10:C} -- Currency",

myDouble);

Console.WriteLine("{0,-10:E3} -- Sci. Notation, 3 dec places",

myDouble);

Console.WriteLine("{0,-10:x} -- Hexadecimal integer",

1194719 );

 

This code produces the following output:

 

 

 

 

 

 

12.345678

-- General

 

 

12.345678

-- Default, same as General

 

 

12.3457

-- Fixed Point, 4 dec places

 

 

$12.35

-- Currency

 

 

1.235E+001

-- Sci. Notation, 3 dec places

 

 

123adf

-- Hexadecimal integer

 

 

 

 

 

 

669

CHAPTER 25 OTHER TOPICS

Standard Numeric Format Specifiers

The Regional and Language Options applet of the Windows Control Panel can affect the resulting formats of some of the specifiers. For example, the currency symbol of the country or region specified will be used by the currency format specifier. You can do the same in code by creating a CultureInfo and assigning it to the Thread.CurrentThread.CurrentCulture property.

Table 25-2 summarizes the nine standard numeric format specifiers. The first column lists the name of the specifier followed by the specifier characters. If the specifier characters have different output depending on their case, they are marked case sensitive.

Table 25-2. Standard Numeric Format Specifiers

Name and

Characters Meaning

Currency

C, c

Formats the value as a currency, using a currency symbol. Precision specifier: The number of decimal places.

Sample: Console.WriteLine("{0 :C}", 12.5);

Output: $12.50

Decimal

D, d

Fixed-point

F, f

General

G, g

Hexadecimal

X, x

Case sensitive

A string of decimal digits, with a negative sign, if appropriate. Can be used only with integral types.

Precision specifier: The minimum number of digits to use in the output string. If the number has fewer digits, it will be padded with 0s on the left.

Sample: Console.WriteLine("{0 :D4}", 12);

Output: 0012

A string of decimal digits with a decimal point. Can also include a negative sign, if appropriate.

Precision specifier: The number of decimal places.

Sample: Console.WriteLine("{0 :F4}", 12.3456789);

Output: 12.3457

A compact fixed-point representation or a scientific notation representation, depending on the value. This is the default, if no specifier is listed.

Precision specifier: Depends on the value.

Sample: Console.WriteLine("{0 :G4}", 12.3456789);

Output: 12.35

A string of hexadecimal digits. The hex digits A through F will match the case of the specifier.

Precision specifier: The minimum number of digits to use in the output string. If the number has fewer digits, it will be padded with 0s on the left.

Sample: Console.WriteLine("{0 :x}", 180026);

Output: 2bf3a

670

CHAPTER 25 OTHER TOPICS

Number

Similar to fixed-point representation but includes separators between each group of

N, n

three digits, starting at the decimal point and going left.

Precision specifier: The number of decimal places.

 

 

Sample: Console.WriteLine("{0 :N2}", 12345678.54321);

 

Output: 12,345,678.54

Percent

A string that represents percent. The number is multiplied by 100.

P, p

Precision specifier: The number of decimal places

 

 

Sample: Console.WriteLine("{0 :P2}", 0.1221897);

 

Output: 12.22 %

Round-trip

R, r

Scientific

E, e

Case sensitive

The output string is chosen so that if the string is converted back to a numeric value using a Parse method, the result will be the original value.

Precision specifier: Ignored.

Sample: Console.WriteLine("{0 :R}", 1234.21897);

Output: 1234.21897

Scientific notation with a mantissa and an exponent. The exponent is preceded by the letter E. The E will be the same case as the specifier.

Precision specifier: The number of decimal places.

Sample: Console.WriteLine("{0 :e4}", 12.3456789);

Output: 1.2346e+001

671

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]