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.

434 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

FOR/NEXT The statement For index = a To b Step s sets the value of the variable index to<br />

a <strong>and</strong> repeatedly executes the statements between itself <strong>and</strong> the statement Next index. Each<br />

time the Next statement is reached, s is added to the value of index. This process continues<br />

until the value of index passes b. Although the numbers a, b, <strong>and</strong> s may have any numeric<br />

type, the lower the precision of the type, the faster the loop executes. The statement For index<br />

= a To b is equivalent to the statement For index = a To b Step 1. The index following the<br />

word Next is optional.<br />

FORECOLOR The property ForeColor determines the color used to display text, captions,<br />

graphics, <strong>and</strong> Printed characters. If the ForeColor property of a form or picture box is<br />

changed, subsequent characters will appear in the new color. For a text box, comm<strong>and</strong> button,<br />

or label, text or caption is immediately changed to the new color. [color]<br />

FORMAT The value of the function Format(expression, str) is a string representing expression<br />

(a number, date, time, or string) formatted according to the rules given by str. Format is<br />

useful when assigning values to the Text property <strong>and</strong> when Printing to a form, picture box,<br />

or the printer.<br />

Numeric output can be formatted with commas, leading <strong>and</strong> trailing zeros, preceding or<br />

trailing signs (+ or –), <strong>and</strong> exponential notation. This is accomplished either by using for str<br />

the name of one of several predefined numeric formats or by combining in str one or more<br />

of the following special numeric formatting characters: #, 0, decimal point (period), comma,<br />

%, E–, <strong>and</strong> E+. The expression to be formatted can evaluate to one of the numeric types or a<br />

string representing a number.<br />

Predefined numeric formats include “General Number,”which displays a number as is;<br />

“Currency,”which displays a number with a leading dollar sign <strong>and</strong> with commas every three<br />

digits to the left of the decimal, displays two decimal places, <strong>and</strong> encloses negative numbers<br />

in parentheses; “Fixed,” which displays two digits to the right <strong>and</strong> at least one digit to the left<br />

of the decimal point; “St<strong>and</strong>ard,”which displays a number with commas <strong>and</strong> two decimal<br />

places but does not use parentheses for negative numbers; “Percent,” which multiplies the<br />

value by 100 <strong>and</strong> displays a percent sign after two decimal places; <strong>and</strong> “Scientific,”which<br />

displays numbers in st<strong>and</strong>ard scientific notation. For example, Format(–5432.352, “Currency”)<br />

gives the string “($5,432.35)”.<br />

The symbol # designates a place for a digit. If the number being formatted does not need<br />

all the places provided by the #’s given in str, the extra #’s are ignored. The symbol 0, like #,<br />

designates a place for a digit. However, if the number being formatted does not need all the<br />

places provided by the 0’s given in str, the character 0 is displayed in the extra places. If the<br />

number being converted has more whole part digits than there is space reserved by #’s <strong>and</strong><br />

0’s, additional space is used as if the format string had more #’s at its beginning. For example,<br />

Format(56, “####”) yields “56”, Format(56, “#”) yields “56”, Format(0, “#”) yields “”,<br />

Format(56, “0000”) yields “0056”, Format(56, “0”) yields “56”, <strong>and</strong> Format(0, “0”) yields<br />

“0”.<br />

The decimal point symbol (.) marks the location of the decimal place. It separates the<br />

format rules into two sections, one applying to the whole part of the number <strong>and</strong> the other to<br />

the decimal part. When included in the format string, a decimal point will always appear in<br />

the resulting string. For example, Format(56.246, “#.##”) yields “56.25”, Format(.246,<br />

“#.##”) yields “.25”, Format(.246, “0.##”) yields “0.25”, <strong>and</strong> Format(56.2, “0.00”) yields<br />

“52.20”.<br />

The comma symbol (,) placed to the left of the decimal point between #’s <strong>and</strong>/or 0’s causes<br />

commas to be displayed to the left of every third digit to the left of the decimal point, as<br />

appropriate. If commas are placed to the immediate left of the decimal point (or to the right<br />

of all #’s <strong>and</strong> 0’s when the decimal-point symbol is not used), then before the number is formatted,<br />

it is divided by 1000 for each comma, but commas will not appear in the result. In<br />

order to divide by201000’s <strong>and</strong> display commas in the result, use format strings like<br />

“#,#,.00”, which displays the number with commas in units of thous<strong>and</strong>s, <strong>and</strong> “#,#,,.00”,<br />

which displays the number with commas in units of millions. For example, Format(1234000,<br />

“#,#”) yields “1,234,000”, Format(1234000, “#,”) yields “1234”, Format(1234000, “#,.”)

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

Saved successfully!

Ooh no, something went wrong!