Sams Teach Yourself Visual Basic 6 in 24 Hours

Sams Teach Yourself Visual Basic 6 in 24 Hours

By Greg Perry

Format Function

Visual Basic cannot read your mind, so it doesn't know how you want numbers displayed in your applications. Although Visual Basic sometimes displays none, one, or two decimal places for currency values, you'll almost always want those currency values displayed to two decimal places with a dollar sign and commas when appropriate.

As with the date and time functions, if you've set your computer's international settings to a country other than the United States, your formatted currency values may differ from those shown here. (This book uses U.S. settings.) Some countries use commas to indicate decimal places, whereas the United States uses the decimal point.

Format() returns a Variant (convertible to a String) datatype formatted to look the way you want. Format() doesn't change a value, but Format() changes the way a value looks. Here is the format of Format():

Format(Expression, strFormat)

Often, you'll assign the result of Format() to other variables and controls. Generally, you'll perform all needed calculations on numeric values before formatting those values. After you've performed the final calculations, you'll then format the values to String (or Variant) datatypes and display the resulting answers as needed.

Expression can be a variable, an expression, or a constant. strFormat must be a value from Table 14.8. Visual Basic contains many format strings in addition to the ones shown in Table 14.8. You can even develop your own programmer-defined format strings, although this book doesn't go into those.

Table 14.8. The strFormat values.

strformat Description
"Currency" Ensures that a dollar sign ($) appears before the formatted value, followed by a thousands separator (a decimal point or comma for values over 999; your country setting determines whether the thousands separator is a comma or a decimal). Two decimal places will always show. Visual Basic displays negative values in parentheses.
"Fixed" Displays at least one digit before and two digits following the decimal point, with no thousands separator.
"General Number" Displays the number with no thousands separator.
"Medium Time" Displays the time in 12-hour format and the a.m. or p.m. indicator.
"On/Off" Displays On if the value contains a nonzero or True value and displays Off if the value contains zero or a False value.
"Percent" Displays the number, multiplied by 100, and adds the percent sign to the right of the number.
"Scientific" Displays numbers in scientific notation.
"Short Time" Displays the time in 24-hour format.
"True/False" Displays True if the value contains a nonzero or True value, and displays False if the value contains zero or a False value.
"Yes/No" Displays Yes if the value contains a nonzero or True value and displays No if the value contains zero or a False value.

Listing 14.2 contains a series of formatting function calls that convert numeric and logical values to formatted Variant datatypes that you can display.

Example 14.2. Formatting numeric and logical values.

 1: Dim FormValue (8) As String
 2: ' Change 12345.678 to $12,345.68
 3: FormValue(1) = Format(12345.678, "Currency")
 4:
 5: ' Change 12345678 to 12345.68
 6: FormValue(2) = Format(12345.678, "Fixed")
 7:
 8: ' Change .52 to 52.00%
 9: FormValue(3) = Format(.52, "Percent")
10:
11: ' Change 1 to Yes
12: FormValue(4) = Format(1, "Yes/No")
13:
14: ' Change 0 to No
15: FormValue(5) = Format(0, "Yes/No")
16:
17: ' Change 1 to True
18: FormValue(6) = Format(1, "True/False")
19:
20: ' Change 0 to False
21: FormValue(7)= Format(0, "True/False")
						
					

An edit mask is a format string, such as "#,###.##", that specifies how you want numeric and string data to appear. For additional information on using special numeric formats, search the online documentation for User-Defined Numeric Formats (Format Function).

Share ThisShare This

Informit Network