180
LESSON 14 Working With StringS
For the C, E, F, N, and P formats, the precision specifier indicates the number of digits after the
decimal point.
Custom Numeric Formats
If the standard numeric formatting characters dont do what you want, you can use a custom
numeric format. Table 14-3 summarizes the custom numeric formatting characters.
TABLE 143
CHARACTER MEANING
0 Digit or zero. A digit is displayed here or a zero if there is no corresponding digit in
the value being formatted.
# Digit or nothing. A digit is displayed here or nothing if there is no corresponding
digit in the value being formatted.
. Decimal separator. The decimal separator goes here. Note that the actual separa-
tor character may not be a period depending on the computer’s regional settings,
although you still use the period in the format string.
, Thousands separator. The thousands separator goes here. The actual separator
character may not be a comma depending on the computer’s regional settings,
although you still use the comma in the format string.
% Percent. The number is multiplied by 100 and the percent sign is added at this point.
For example, %0 puts the percent sign before the number and 0% puts it after.
E+0 Scientific notation. The number of 0s indicates the number of digits in the exponent.
If + is included, the exponent always includes a + or – sign. If + is omitted, the expo-
nent only includes a sign if the exponent is negative. For example, the format string
#.##E+000 used with the value 1234.56 produces the result 1.23E+003.
Escape character. Whatever follows the is displayed without any conversion. For
example, the format 0.00\% would add a percent sign to a number without scaling it
by 100 as the format 0.00% does. Note that you must escape the escape character
itself in a normal (non-verbatim) string. For example, a format string might look like
{0:0.00\%} in the code.
‘ABC’ Literal string. Characters enclosed in single or double quotes are displayed without
any conversion.
; Section separator. See the following text.
You can use a section separator to divide a formatting string into two or three sections. If you use
two sections, the first applies to values greater than or equal to zero, and the second section applies
to values less than zero. If you use three sections, they apply to values that are greater than, less
than, and equal to zero, respectively.
596906c14.indd 180 4/7/10 12:32:54 PM
Format and ToString
181
For example, Table 14-4 shows the result produced by the three-section custom formatting string
“{0:$#,##0.00;($#,##0.00);-- zero --}“ for different values.
TABLE 144
VALUE FORMATTED RESULT
12345.678 $12,345.68
-12345.678 ($12,345.68)
0.000 -- zero --
Standard Date and Time Formats
Just as numeric values have standard and custom formatting strings, so too do dates and times.
Table 14-5 summarizes the standard date and time formatting patterns. The examples are those
produced for 1:23 PM August 20, 2010 on my computer set up for US English. Your results will
depend on how your computer is configured. Note that for many of the characters in this table, the
uppercase and lowercase versions have different meanings.
TABLE 145
CHARACTER MEANING EXAMPLE
d Short date 8/20/2010
D Long date Friday, August 20, 2010
f Full date, short time Friday, August 20, 2010 1:23 PM
F Full date, long time Friday, August 20, 2010 1:23:00 PM
g General date/time, short time 8/20/2010 1:23 PM
G General date/time, long time 8/20/2010 1:23:00 PM
M or m Month day August 20
O Round trip 2010-08-20T13:23:00.0000000
R or r RFC1123 Fri, 20 Aug 2010 13:23:00 GMT
s Sortable date/time 2010-08-20T13:23:00
t Short time 1:23 PM
T Long time 1:23:00 PM
u Universal sortable short date/time 2010-08-20 13:23:00Z
continues
596906c14.indd 181 4/7/10 12:32:54 PM
182
LESSON 14 Working With StringS
CHARACTER MEANING EXAMPLE
U Universal sortable full date/time Friday, August 20, 2010 1:23:00 PM
Y or y Year month August, 2010
The DateTime class also provides several methods that return the date’s value as a string formatted
in the most common date and time formats. Table 14-6 summarizes the most useful of these methods
and shows the results on my computer set up for US English. Your results will depend on how your
computer is configured.
TABLE 146
METHOD FORMAT EXAMPLE
ToLongDateString Long date (D) Friday, August 20, 2010
ToLongTimeString Long time (T) 1:23:00 PM
ToShortDateString Short date (d) 8/20/2010
ToShortTimeString Short time (t) 1:23 PM
ToString General date and time (G) 8/20/2010 1:23:00 PM
Custom Date and Time Formats
If the standard date and time formatting characters dont do the trick, you can use a custom format.
Table 14-7 summarizes the custom date and time formatting strings. Note that for many of the char-
acters in this table, the uppercase and lowercase versions have different meanings.
TABLE 147
CHARACTER MEANING
d Day of month between 1 and 31.
dd Day of month between 01 and 31.
ddd Abbreviated day of week (Mon, Tue, and so on).
dddd Full day of week (Monday, Tuesday, and so on).
f Digits after the decimal for seconds. For example,  means use four digits.
F Similar to f but trailing zeros are not displayed.
g Era specifier. For example, A.D.
TABLE 145
(continued)
596906c14.indd 182 4/7/10 12:32:54 PM
Format and ToString
183
CHARACTER MEANING
h Hours between 1 and 12.
hh Hours between 01 and 12.
H Hours between 0 and 23.
HH Hours between 00 and 23.
m Minutes between 1 and 59.
mm Minutes between 01 and 59.
M Month between 1 and 12.
MM Month between 01 and 12.
MMM Month abbreviation (Jan, Feb, and so on).
MMMM Month name (January, February, and so on).
s Seconds between 1 and 59.
ss Seconds between 01 and 59.
t First character of AM/PM designator.
tt AM/PM designator.
y One- or two-digit year. If the year has fewer than two digits, is it not zero padded.
yy Two-digit year, zero padded if necessary.
yyy Three-digit year, zero padded if necessary.
yyyy Four-digit year, zero padded if necessary.
yyyyy Five-digit year, zero padded if necessary.
z Signed time zone oset from GMT. For example, Pacific Standard Time is –8.
zz Signed time zone oset from GMT in two digits. For example, Pacific Standard
Time is –08.
zzz Signed time zone oset from GMT in hours and minutes. For example, Pacific
Standard Time is –08:00.
: Hours, minutes, and seconds separator.
/ Date separator.
‘ABC’ Literal string. Characters enclosed in single or double quotes are displayed
without any conversion.
596906c14.indd 183 4/7/10 12:32:54 PM
184
LESSON 14 Working With StringS
Table 14-8 shows some example formats and their results. The date used was 1:23:45.678 PM
August 20, 2010 on my computer set up for US English. Your results will depend on how your
computer is configured.
TABLE 148
FORMAT RESULT
M/d/yy 8/20/10
d MMM yy 20 Aug 10
HH:mm ‘hours’ 13:23 hours
h:mm:ss., M/d/y 1:23:45.67, 8/20/10
dddd ‘at’ h:mmt Friday at 1:23P
ddd ‘at’ h:mmt Fri at 1:23PM
TRY IT
In this Try It, you build a program that displays the
current date and time in a
Label when it starts as shown
in Figure 14-1.
Lesson Requirements
In this Try It:
Start a new project and add a
Label to its form.
Give the form a
Load event handler that sets the Label’s text as shown in Figure 14-1.
You can download the code and resources for this Try It from the book’s web
page at
www.wrox.com or www.CSharpHelper.com/24hour.html. The programs
can be found within the Lesson14 folder.
Hints
The
DateTime.Now property returns the current date and time.
Either use
string.Format or the value’s ToString method to format the result.
FIGURE 141
596906c14.indd 184 4/7/10 12:32:55 PM
..................Content has been hidden....................

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