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

Create successful ePaper yourself

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

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

last = Right(nom, Len(nom) - n)<br />

picResults.Print “Your first name is ”; first<br />

picResults.Print “Your last name has”; Len(last); “letters.”<br />

End Sub<br />

[Run, type John Doe into the text box, <strong>and</strong> then click the comm<strong>and</strong> button.]<br />

■ FORMAT FUNCTIONS<br />

The Format functions are used to display numbers <strong>and</strong> dates in familiar forms <strong>and</strong> to right-justify<br />

numbers. Here are some examples of how numbers are converted to strings with Format<br />

functions.<br />

Function String Value<br />

FormatNumber(12345.628, 1) 12,345.6<br />

FormatCurrency(12345.628, 2) $12,345.63<br />

FormatPercent(.185, 2) 18.50%<br />

The value of FormatNumber(n, r) is the string containing the number n rounded to r decimal<br />

places <strong>and</strong> displayed with commas every three digits to the left of the decimal point. The<br />

value of FormatCurrency(n, r) is the string consisting of a dollar sign followed by the value<br />

of FormatNumber(n, r). FormatCurrency uses the accountant’s convention of using surrounding<br />

parentheses to denote negative amounts. The value of FormatPercent(n, r) is the<br />

string consisting of the number n displayed as a percent <strong>and</strong> rounded to r decimal places.<br />

With all three functions, r can be omitted. If so, the number is rounded to 2 decimal<br />

places. Strings corresponding to numbers less than 1 in magnitude have a zero to the left of<br />

the decimal point. Also, n can be a either a number, a numeric expression, or even a string<br />

corresponding to a number.<br />

Function String Value<br />

FormatNumber(1 + Sqr(2), 3) 2.414<br />

FormatCurrency(-1000) ($1,000.00)<br />

FormatPercent(“.005”) 0.50%<br />

If dateString represents a date in a form such as “7-4-1999”, “7-4-99”, or “7/4/99”, then<br />

the value of FormatDateTime(dateString, vbLongDate) is a string giving the date as Sunday,<br />

July 04, 1999.<br />

Function String Value<br />

FormatDateTime(“9-15-99”, vbLongDate) Wednesday, September 15, 1999<br />

FormatDateTime(“10-23-00”, vbLongDate) Monday, October 23, 2000<br />

The value of Format(expr, “@@ . . . @”), where “@@ . . . @” is a string of n “at” symbols,<br />

is the string consisting of the value of expr right-justified in a field of n spaces. This<br />

function is used with fixed-width fonts, such as Courier or Terminal, to display columns of<br />

numbers so that the decimal points <strong>and</strong> commas are lined up or to display right-justified lists<br />

of words. The following examples use a string of 10 “at” symbols.<br />

Function String Value<br />

Format(1234567890, “@@@@@@@@@@”) 1234567890<br />

Format(FormatNumber(1234.5), “@@@@@@@@@@”) 1,234.50<br />

Format(FormatNumber(12345.67), “@@@@@@@@@@”) 12,345.67<br />

Format(FormatCurrency(13580.17), “@@@@@@@@@@”) $13,580.17

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

Saved successfully!

Ooh no, something went wrong!