CST2601 Visual Basic I
Notes on Format function


Formatting Numbers, Dates, and Times

Visual Basic provides great flexibility in displaying number formats, as well as date and time formats. You can easily display international formats for numbers, dates, and times.

The Format function converts the numeric value to a text string and gives you control over the string’s appearance. For example, you can specify the number of decimal places, leading or trailing zeros, and currency formats. The syntax is:

Format(expression[, format[, firstdayofweek[, firstweekofyear]]])

The expression argument specifies a number to convert, and the format argument is a string made up of symbols that shows how to format the number. The most commonly used symbols are listed in the table below.

Symbol Description
0 Digit placeholder; prints a trailing or a leading zero in this position, if appropriate.
# Digit placeholder; never prints trailing or leading zeros.
. Decimal placeholder.
, Thousands separator.
– + $ ( ) space Literal character; characters are displayed exactly as typed into the format string.
 

The firstdayofweek argument is a constant that specifies the first day of the week; the firstweekofyear argument is a constant that specifies the first week of the year. Both arguments are optional. For more information about these constants, see "Format Function" in the Language Reference.

Named Formats

Visual Basic provides several standard formats to use with the Format function. Instead of designating symbols in the format argument, you specify these formats by name in the format argument of the Format function. Always enclose the format name in double quotation marks ("").

The following table lists the format names you can use.

Named format Description
General Number Displays number with no thousand separator.
Currency Displays number with thousand separator, if appropriate; display two digits to the right of the decimal separator. Output is based on user's system settings.
Fixed Displays at least one digit to the left and two digits to the right of the decimal separator.
Standard Displays number with thousand separator, at least one digit to the left and two digits to the righseparator.
Percent Multiplies the value by 100 with a percent sign at the end.
Scientific Uses standard scientific notation.
General Date Shows date and time if expression contains both. If expression is only a date or a time, the missing information is not displayed. Date display is determined by user's system settings.
Long Date Uses the Long Date format specified

by user's system settings.

Medium Date Uses the dd-mmm-yy format (for example, 03-Apr-93). Date display is determined by user's system settings.
Short Date Uses the Short Date format specified by user's system settings.
Long Time Displays a time using user's system's long-time format; includes hours, minutes, seconds.
Medium Time Shows the hour, minute, and "AM" or "PM" using the "hh:mm AM/PM" format.
Short Time Shows the hour and minute using the hh:mm format.
Yes/No Any nonzero numeric value (usually –1) is Yes. Zero is No.
True/False Any nonzero numeric value (usually –1) is True. Zero is False.
On/Off Any nonzero numeric value (usually –1) is On. Zero is Off.

The Format function supports many other special characters, such as the percentage placeholder and exponents.

For More Information   See "Format Function" in the Language Reference.

Number Formats

The following number conversions assume that the country in the Windows Control Panel is set to "English (United States)."

Format syntax Result
Format(8315.4, "00000.00") 08315.40
Format(8315.4, "#####.##") 8315.4
Format(8315.4, "##,##0.00") 8,315.40
Format(315.4,"$##0.00") $315.40

The symbol for the decimal separator is a period (.), and the symbol for the thousands separator is a comma (,). However, the separator character that is actually displayed depends on the country specified in the Windows Control Panel.

Printing Formatted Dates and Times

To print formatted dates and times, use the Format function with symbols representing date and time. These examples use the Now and Format functions to identify and format the current date and time. The following examples assume that the Regional Settings dialog box of the Windows Control Panel is set to "English(United States)".

Format syntax Result
Format(Now, "m/d/yy") 1/27/93
Format(Now, "dddd, mmmm dd, yyyy") Wednesday, January 27, 1993
Format(Now, "d-mmm") 27-Jan
Format(Now, "mmmm-yy") January-93
Format(Now, "hh:mm AM/PM") 07:18 AM
Format(Now, "h:mm:ss a/p") 7:18:00 a
Format(Now, "d-mmmm h:mm") 3-January 7:18

By using the Now function with the format "ddddd" and "ttttt, " you can print the current date and time in a format appropriate for the selection in the Regional Settings dialog box of the Windows Control Panel.

Country Format syntax Result
Sweden Format(Now, "ddddd ttttt") 1992-12-31 18.22.38
United Kingdom Format(Now, "ddddd ttttt") 31/12/92 18:22:38
Canada (French) Format(Now, "ddddd ttttt") 92-12-31 18:22:38
United States Format(Now, "ddddd ttttt") 12/31/92 6:22:38 PM

For More Information   For more information about international considerations when using the Format function, see "Locale-Aware Functions" in "International Issues. " For more information about dates based on system locale, see "Writing International Code in Visual Basic" in "International Issues. "