15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

examining system.string ❘ 213<br />

As you probably know, you need to specify the format in which you want a variable displayed when you<br />

call Console.WriteLine(). Therefore, this section uses this method as an example, although most of the<br />

discussion applies to any situation in which you want to format a string. For example, if you want to display<br />

the value of a variable in a list box or text box, you will normally use the String.Format() method to<br />

obtain the appropriate string representation of the variable. However, the actual format specifiers you use to<br />

request a particular format are identical to those passed to Console.WriteLine(). Hence, you will focus<br />

on Console.WriteLine() as an example. You start by examining what actually happens when you supply<br />

a format string to a primitive type, <strong>and</strong> from this, you will see how you can plug format specifiers for your<br />

own classes <strong>and</strong> structs into the process.<br />

Chapter 2, “Core <strong>C#</strong>,” uses format strings in Console.Write() <strong>and</strong> Console.WriteLine() like this:<br />

double d = 13.45;<br />

int i = 45;<br />

Console.WriteLine("The double is {0,10:E} <strong>and</strong> the int contains {1}", d, i);<br />

The format string itself consists mostly of the text to be displayed, but wherever there is a variable to be<br />

formatted, its index in the parameter list appears in braces. You might also include other information inside<br />

the braces concerning the format of that item. For example, you can include:<br />

➤<br />

➤<br />

The number of characters to be occupied by the representation of the item, prefixed by a comma. A<br />

negative number indicates that the item should be left-justified, whereas a positive number indicates<br />

that it should be right-justified. If the item actually occupies more characters than have been<br />

requested, it will still appear in full.<br />

A format specifier, preceded by a colon. This indicates how you want the item to be formatted. For<br />

example, you can indicate whether you want a number to be formatted as a currency or displayed in<br />

scientific notation.<br />

The following table lists the common format specifiers for the numeric types, which were briefly discussed<br />

in Chapter 2.<br />

sPeCifier aPPlies To meaning eXamPle<br />

C Numeric types Locale-specific monetary<br />

value<br />

D Integer types only General integer 4834<br />

E Numeric types Scientific notation 4.834E+003<br />

F Numeric types Fixed-point decimal 4384.50<br />

G Numeric types General number 4384.5<br />

N Numeric types Common locale-specific<br />

format for numbers<br />

P Numeric types Percentage notation 432,000.00%<br />

$4834.50 (USA) £4834.50 (UK)<br />

4,384.50 (UK/USA)<br />

4 384,50 (continental Europe)<br />

X Integer types only Hexadecimal format 1120 (If you want to display 0x1120, you<br />

will have to write out the 0x separately)<br />

If you want an integer to be padded with zeros, you can use the format specifier 0 (zero) repeated as many<br />

times as the number length is required. For example, the format specifier 0000 will cause 3 to be displayed<br />

as 0003, <strong>and</strong> 99 to be displayed as 0099, <strong>and</strong> so on.<br />

It is not possible to give a complete list, because other data types can add their own specifiers. Showing how<br />

to define your own specifiers for your own classes is the aim of this section.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!