The Format$ Function – Part 1

(Named Formats)

 

The Format$ function returns a string containing an expression formatted according to instructions contained in a format expression. This function is useful for formatting numeric, date, time, and string data.

 

Note: The "Format" function as well as any VB function that returns a string can be coded with or without the "$" at the end. It is more efficient to use the "$" – when the "non-$" version of the function is used, it returns a Variant which must be converted to a string.

 

Syntax:

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

 

The Format$ function syntax has these parts:

 

Part

Description

expression

Required. Any valid expression.

format

Optional. A string containing a valid named or user-defined format expression. When omitted, Format$ provides functionality similar to the CStr function that converts a number to a string.

firstdayofweek and firstweekofyear

Optional. Constants that can be used  to override defaults when formatting date values. These will not be used in any example presented in this section.

 

 

Named Numeric Formats

 

The Format$ function recognizes the following Named Numeric formats:

 

Named Numeric Format

Description

General Number

Displays the number as entered, with no rounding and no commas.

Currency

Displays the number with a dollar sign, comma (if appropriate), and  two digits to the right of the decimal point. Shows negative numbers inside parentheses.

Fixed

Displays the number with at least one digit to the left of the decimal separator and two digits to the right. Does not show comma.

Standard

Displays the number with at least one digit to the left of the decimal separator and two digits to the right and commas (if appropriate).

Percent

Multiplies the value by 100 and displays the result with two digits to the right of the decimal point and a percent sign at the end.

Scientific

Uses standard scientific notation.

Yes/No

Any nonzero numeric value is Yes. Zero is No.

True/False

Any nonzero numeric value is True. Zero is False.

On/Off

Any nonzero numeric value is On. Zero is Off.

 

To demonstrate the named numeric formats described above, set up another "Try It" project, and place the following code in the cmdTryIt_Click event:

 

Private Sub cmdTryIt_Click()

 

    Dim strUserInput    As String

    Dim dblTestNumber   As Double

 

    strUserInput = InputBox("Please enter a number to demonstrate named numeric formats:")

    dblTestNumber = Val(strUserInput)

 

    Print "Using General Number:"; Tab(25); Format$(dblTestNumber, "General Number")

    Print "Using Currency:"; Tab(25); Format$(dblTestNumber, "Currency")

    Print "Using Fixed:"; Tab(25); Format$(dblTestNumber, "Fixed")

    Print "Using Standard:"; Tab(25); Format$(dblTestNumber, "Standard")

    Print "Using Percent:"; Tab(25); Format$(dblTestNumber, "Percent")

    Print "Using Scientific:"; Tab(25); Format$(dblTestNumber, "Scientific")

    Print "Using Yes/No:"; Tab(25); Format$(dblTestNumber, "Yes/No")

    Print "Using True/False:"; Tab(25); Format$(dblTestNumber, "True/False")

    Print "Using On/Off:"; Tab(25); Format$(dblTestNumber, "On/Off")

 

End Sub

 

Run the project and click the "Try It" button. When the input box comes up, enter 123456.789, as shown below:

 

 

The number you entered, formatted in the various ways, will be displayed on your form:

 

 

 

Test the code using a variety of different values, including negative numbers and zero.

 

Download the VB project code for the example above here.

 

 

Named Date & Time Formats

The Format$ function recognizes the following Named Date and Time formats:

 

Named Date/Time Format

Description

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.

Long Date

Uses the Long Date format specified in the Regional Settings dialog box of the Microsoft Windows Control Panel.

Medium Date

Uses the dd-mmm-yy format (for example, 03-Apr-93)

Short Date

Uses the Short Date format specified in the Regional Settings dialog box of the Windows Control Panel.

Long Time

Shows the hour, minute, second, and "AM" or "PM" using the h:mm:ss format.

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.

 

This next example uses the Now keyword. There are three VB keywords related to the current date and/or time:

 

            Now     Returns the current date and time

 

            Date     Returns the current date

 

            Time    Returns the current time

 

To demonstrate the named date and time formats described above, set up another "Try It" project, and place the following code in the cmdTryIt_Click event:

 

Private Sub cmdTryIt_Click()

 

    Print "Using General Date:"; Tab(25); Format$(Now, "General Date")

    Print "Using Long Date:"; Tab(25); Format$(Now, "Long Date")

    Print "Using Medium Date:"; Tab(25); Format$(Now, "Medium Date")

    Print "Using Short Date:"; Tab(25); Format$(Now, "Short Date")

    Print "Using Long Time:"; Tab(25); Format$(Now, "Long Time")

    Print "Using Medium Time:"; Tab(25); Format$(Now, "Medium Time")

    Print "Using Short Time:"; Tab(25); Format$(Now, "Short Time")

 

End Sub

 

Run the project and click the "Try It" button. The current date and/or time, formatted in the various ways, will be displayed on your form:

 

 

 

Download the VB project code for the example above here.