Information stored in variables as various data types may not be in a format for visual presentation on a Web page. Visual Basic provides several built-in functions for presentation of this data.
Formatting Currency
The FormatCurrency() function formats numeric data for presentation as dollars and cents. Its general format is shown below.
value is any expression that produces a number; trailing digits is an integer giving the number of digits following the decimal point; the default is rounding to 2 digits; leading digit is True or False to indication whether a leading 0 is to appear before the decimal point for fractional values; parentheses is True or False to indicate whether negative numbers should be displayed inside parentheses; group digits is True or False to indicate whether numbers should be grouped between commas.
Formatting Numbers
The FormatNumber() function returns a value formatted as a number. Its general format is shown below.
Formatting Percentages
The FormatPercent() function returns a value formatted as a percentage, that is, multiplied by 100 with a trailing % character. Its general format is shown below.
Formatting Dates and Times
The FormatDateTime() function returns a string expression representing a date/time value. Its general format is
where value is a date or time value and format is one of the following values: GeneralDate, LongDate, ShortDate, LongTime, or ShortTime.
The Format() Function
The Format() function is a general-purpose formatting function that returns a string value formatted according to a format string. The format strings duplicate numeric and date/time formats produced by the specialized formats described above. The general format for applying the Format() function is shown below.
A format string for numeric values can use one of the predefined string values shown in the following table.
Examples of applying a format string to numeric values are shown in the following table.
A format string for date/time values can use one of the predefined string values shown in the following table.
Examples of applying a format string to date/time values are shown in the following table.
User-defined Numeric Formats
Formats can be defined for displaying numeric values by composing a string to describe the format. This user-defined string is applied through the Format() function. The characters shown in the following table are used to compose the format string.
Examples of applying a user-defined format string to numeric values are shown in the following table.
User-defined Date/Time Formats
Formats can be defined for displaying date and time values by composing a string to describe the format. This user-defined string is applied through the Format() function. The characters shown in the following table are used to compose the date/time format string.
Examples of applying a user-defined format string to date/time values are shown in the following table.