19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

yields “1234.”, Format(1234000, “#,,.0”) yields “1.2”, <strong>and</strong> Format(1234000, “#,0,.0”) yields<br />

“1,234.0”.<br />

The percent symbol (%) placed to the right of all #’s, 0’s, <strong>and</strong> any decimal point causes<br />

the number to be converted to a percentage (multiplied by 100) before formatting <strong>and</strong> the<br />

symbol % to be displayed. For example, Format(.05624, “#.##%”) yields “5.62%”, <strong>and</strong> Format(1.23,<br />

“#%”) yields “123%”.<br />

The symbols E+ <strong>and</strong> E– placed to the right of all #’s, 0’s, <strong>and</strong> any decimal point cause<br />

the number to be displayed in scientific notation. Places for the digits in the exponent must<br />

be reserved to the right of E+ or E– with #’s or 0’s. When E+ is used <strong>and</strong> the exponent is positive,<br />

a plus sign appears in front of the exponent in the result. When E– is used <strong>and</strong> the exponent<br />

is positive, no sign or space precedes the exponent. When scientific notation is used,<br />

each position reserved by #’s to the left of the decimal point is used whenever possible. For<br />

example, Format(1234.56, “#.##E+##”) yields “1.23E+3”, Format(1234.56, “##.##E–##”)<br />

yields “12.34E2”, Format(1234, “###.00E+##”) yields “123.40E+1”, <strong>and</strong> Format(123,<br />

“###E+00”) yields “123E+00”.<br />

Date <strong>and</strong> time output can be formatted using numbers or names for months, putting the<br />

day, month, <strong>and</strong> year in any order desired, using 12-hour or 24-hour notation, <strong>and</strong> so on. This<br />

is accomplished either by letting str be the name of one of several predefined date/time formats<br />

or by combining in str one or more special date/time formatting characters. The expression<br />

to be formatted can evaluate to a number that falls within the range of valid serial dates<br />

or to a string representing a date/time.<br />

Predefined date/time formats include “General Date,” which displays a date in<br />

mm/dd/yyyy format <strong>and</strong>, if appropriate, a time in hh:mm:ss PM format; “Long Date,”which<br />

displays the day of week, the full name of month, the day, <strong>and</strong> a four-digit year; “Medium<br />

Date,”which displays the day, abbreviated month name, <strong>and</strong> two-digit year; “Short<br />

Date,”which displays “mm/dd/yy”; “Long Time,”which displays the time in hh:mm:ss PM<br />

format; “Medium Time,”which displays time in hh:mm PM format; <strong>and</strong> “Short Time,”which<br />

display time in 24-hour format as hh:mm. For example, let dt = DateSerial(55,2,10) +<br />

Time~Serial(21,45,30). Then Format(dt, “General Date”) yields “2/10/55 9:45:30 PM”, Format(dt,<br />

“Medium Date”) yields “10-Feb-55”, <strong>and</strong> Format(dt, “Short Time”) yields “21:45”.<br />

Format symbols for the day, month, <strong>and</strong> year include d (day as number but no leading<br />

zero), dd (day as number with leading zero), ddd (day as three-letter name), dddd (day as full<br />

name), m (month as number but no leading zero), mm (month as number with leading zero),<br />

mmm (month as three-letter name), mmmm (month as full name), yy (year as two-digit number),<br />

<strong>and</strong> yyyy (year as four-digit number). Separators such as slash, dash, <strong>and</strong> period may<br />

be used as desired to connect day, month, <strong>and</strong> year symbols into a final format. For example,<br />

Format(“August 13, 1958”, “dddd, d.mmm.yy”) yields “Wednesday, 13.Aug.58” <strong>and</strong> Format(“July<br />

4, 1776”, “ddd: mmmm dd, yyyy”) yields “Thu: July 04, 1776”. Additional format<br />

symbols for dates include w (day-of-week as number 1–7), ww (week-of-year as number<br />

1–53), q (quarter-of-year as number 1–4), y (day-of-year as number 1–366), ddddd (same as<br />

short date), <strong>and</strong> dddddd (same as long date).<br />

Format symbols for the second, minute, <strong>and</strong> hour include s (seconds with no leading<br />

zero), ss (seconds as two-digit number), n (minutes with no leading zero), nn (minutes as<br />

two-digit number), h (hours with no leading zero), hh (hours as two-digit number), AM/PM<br />

(use 12-hour clock <strong>and</strong> uppercase), am/pm (use 12-hour clock <strong>and</strong> lowercase), A/P (use 12hour<br />

clock <strong>and</strong> single uppercase letter), a/p (use 12-hour clock <strong>and</strong> single lowercase letter),<br />

<strong>and</strong> ttttt (same as general date). Separators such as colons <strong>and</strong> periods may be used as desired<br />

to connect hour, minute, <strong>and</strong> second symbols into a final format. For example, Format(“14:04:01”,<br />

“h:nn AM/PM”) yields “2:04 PM”, Format(“14:04:01”, “h.n.s”) yields<br />

“14.4.1”, <strong>and</strong> Format(0.75, “h:nna/p”) yields “6:00p”.<br />

String output can be formatted as all uppercase, all lowercase, left-justified or right-justified.<br />

Symbols used to format strings are @ (define a field for at least as many characters<br />

as there are @ symbols; if less characters than @ symbols, fill remainder of field with<br />

spaces; if more characters than @ symbols, display the extra characters—don’t clip), &<br />

(reserve space for entire output string), < (convert all characters to lowercase before dis-<br />

Appendix C 435

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

Saved successfully!

Ooh no, something went wrong!