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.
| FormatCurrency(value [, trailing digits] [, leading digit] [, parentheses] [, group digits]) |
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.
| Format | Output |
| FormatCurrency(12345.6789) |
$12,345.68 |
| FormatCurrency(12345.6789, 4) |
$12,345.6789 |
| FormatCurrency(12345.6789,,,,False) |
$12345.68 |
| FormatCurrency(-12345.6789) |
($12,345.68) |
| FormatCurrency(-12345.6789,,,False) |
-$12,345.68 |
| FormatCurrency(.6789) |
$0.68 |
| FormatCurrency(.6789,,False) |
$.68 |
| FormatCurrency(-.6789,,False,False) |
-$.68 |
Formatting Numbers
The FormatNumber() function returns a value formatted as a number. Its general format is shown below.
| FormatNumber(value [, trailing digits] [, leading digit] [, parentheses] [, group digits]) |
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.
| Format | Output |
| FormatNumber(12345.6789) |
12,345.68 |
| FormatNumber(12345.6789,5) |
12,345.67890 |
| FormatNumber(12345.6789,,,,False) |
12345.68 |
| FormatNumber(-12345.6789) |
-12,345.68 |
| FormatNumber(-12345.6789,,,True) |
(12,345.68) |
| FormatNumber(.6789) |
0.68 |
| FormatNumber(.6789,,False) |
.68 |
| FormatNumber(-.6789,4) |
-0.6789 |
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.
| FormatPercent(value [, trailing digits] [, leading digit] [, parentheses] [, group digits]) |
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.
| Format | Output |
| FormatPercent(.6789) |
67.89% |
| FormatPercent(.6789,4) |
67.8900% |
| FormatPercent(-.6789) |
-67.89% |
| FormatPercent(-.6789,,,True) |
(67.89%) |
Formatting Dates and Times
The FormatDateTime() function returns a string expression representing a date/time value. Its general format is
| FormatDateTime(value [, DateFormat.format]) |
where value is a date or time value and format is one of the following values: GeneralDate, LongDate, ShortDate, LongTime, or ShortTime.
| Format | Output |
| FormatDateTime(Now) |
8/30/2010 2:25:32 AM |
| FormatDateTime(Today) |
8/30/2010 |
| FormatDateTime(TimeOfDay) |
2:25:32 AM |
| FormatDateTime(Now,DateFormat.LongDate) |
Monday, August 30, 2010 |
| FormatDateTime(Today,DateFormat.LongDate) |
Monday, August 30, 2010 |
| FormatDateTime(Now,DateFormat.ShortDate) |
8/30/2010 |
| FormatDateTime(Today,DateFormat.ShortDate) |
8/30/2010 |
| FormatDateTime(Now,DateFormat.LongTime) |
2:25:32 AM |
| FormatDateTime(TimeOfDay,DateFormat.LongTime) |
2:25:32 AM |
| FormatDateTime(Now,DateFormat.ShortTime) |
02:25 |
| FormatDateTime(TimeOfDay,DateFormat.ShortTime) |
02:25 |
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 theFormat() function is shown below.
| Format(value, "format string") |
Formatting Numbers
A format string for numeric values can use one of the predefined string values shown in the following table.
| String | Description |
| General Number|G|g |
Displays number with no thousand separator. |
| Currency|C|c |
Displays number with thousand separator, if appropriate; display two digits to the right of the decimal separator. |
| Fixed|F|f |
Displays at least one digit to the left and two digits to the right of the decimal separator. |
| Standard|N|n |
Displays number with thousand separator, at least one digit to the left and two digits to the right of the decimal separator. |
| Percent |
Displays number multiplied by 100 with a percent sign (%) appended immediately to the right; always displays two digits to the right of the decimal separator. |
| P|p |
Displays number with thousandths separator multiplied by 100 with a percent sign (%) appended to the right and separated by a single space; always displays two digits to the right of the decimal separator. |
Examples of applying a format string to numeric values are shown in the following table.
| Format | Output |
| Format(12345.6789,"g") |
12345.6789 |
| Format(12345.6789,"c") |
$12,345.68 |
| Format(12345.6789,"f") |
12345.68 |
| Format(12345.6789,"n") |
12,345.68 |
| Format(-12345.6789,"g") |
-12345.6789 |
| Format(-12345.6789,"c") |
($12,345.68) |
| Format(-12345.6789,"f") |
-12345.68 |
| Format(-12345.6789,"n") |
-12,345.68 |
| Format(.6789,"Percent") |
67.89% |
| Format(.6789,"p") |
67.89 % |
| Format(-.6789,"Percent") |
-67.89% |
| Format(-.6789,"p") |
-67.89 % |
Formatting Dates and Times
A format string for date/time values can use one of the predefined string values shown in the following table.
| String | Description |
| Long Date|D |
Displays a date in long date format. |
| Short Date|d |
Displays a date in short date format. |
| Long Time|T |
Displays a date in long date format. |
| Short Time|t |
Displays a date in short date format. |
| F |
Displays the long date and long time. |
| f |
Displays the long date and short time. |
| g |
Displays the short date and short time. |
| M|m |
Displays the month and the day of a date. |
| Y|y |
Formats the date as the year and month. |
Examples of applying a format string to date/time values are shown in the following table.
| Format | Output |
| Format(Now,"D") |
Monday, August 30, 2010 |
| Format(Now,"d") |
8/30/2010 |
| Format(Now,"T") |
2:25:32 AM |
| Format(Now,"t") |
2:25 AM |
| Format(Now,"F") |
Monday, August 30, 2010 2:25:32 AM |
| Format(Now,"f") |
Monday, August 30, 2010 2:25 AM |
| Format(Now,"g") |
8/30/2010 2:25 AM |
| Format(Now,"m") |
August 30 |
| Format(Now,"y") |
August, 2010 |
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.
| Character | Description |
| 0 |
Digit placeholder. Displays a digit or a zero. If the value has a digit in the position, then it displays; otherwise, a zero is displayed. |
| # |
Digit placeholder. Displays a digit or a space. If the value has a digit in the position, then it displays; otherwise, a space is displayed. |
| . |
Decimal placeholder; determines how many digits are displayed to the left and right of the decimal separator. |
| , |
Thousand separator; separates thousands from hundreds within a number that has four or more places to the left of the decimal separator. Only a single "," is required in the format, between the first set of digit placeholders. |
| % |
Percent placeholder. Multiplies the expression by 100. The percent character (%) is inserted in the position where it appears in the format string. |
| - + $ ( ) |
Literal characters; displayed exactly as typed in the format string. |
Examples of applying a user-defined format string to numeric values are shown in the following table.
| Format | Output |
| Format(012345.6789,"0.00") |
12345.68 |
| Format(012345.6789,"0,0.000") |
12,345.679 |
| Format(012345.6789,"00000,0.000000") |
012,345.678900 |
| Format(012345.6789,"#.##") |
12345.68 |
| Format(012345.6789,"#,#.##") |
12,345.68 |
| Format(012345.6789,"$ #,#.##") |
$ 12,345.68 |
| Format(-012345.6789,"#,#.####") |
-12,345.6789 |
| Format(-012345.6789,"$#,#.##") |
-$12,345.68 |
| Format(.6789,"#,#.##") |
.68 |
| Format(.6789,"0,0.000") |
00.679 |
| Format(-.6789," 0.0000") |
- 0.6789 |
| Format(.6789,"0.00%") |
67.89% |
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.
| Character | Description |
| : |
Time separator. |
| / - |
Date separators. |
| % |
Precedes a single-character format string. |
| d |
Displays the day as a number without a leading zero. |
| dd |
Displays the day as a number with a leading zero. |
| ddd |
Displays the day name as an abbreviation. |
| dddd |
Displays the day as a full name. |
| M |
Displays the month as a number without a leading zero. |
| MM |
Displays the month as a number with a leading zero. |
| MMM |
Displays the month name as an abbreviation. |
| MMMM |
Displays the month as a full name. |
| yy |
Displays the year in two-digit format. |
| yyyy |
Displays the year in four-digit format. |
| h |
Displays the hour as a number without leading zeros using the 12-hour clock. |
| hh |
Displays the hour as a number with leading zeros using the 12-hour clock. |
| H |
Displays the hour as a number without leading zeros using the 24-hour clock. |
| HH |
Displays the hour as a number with leading zeros using the 24-hour clock. |
| m |
Displays the minute as a number without leading zeros. |
| mm |
Displays the minute as a number with leading zeros. |
| s |
Displays the seconds as a number without leading zeros. |
| ss |
Displays the seconds as a number with leading zeros. |
| f... |
Displays fractions of seconds using up to 7 characters to display fractional digits. |
| tt |
Uses the 12-hour clock and displays an uppercase AM with any hour before noon; displays an uppercase PM with any hour between noon and 11:59 P.M. |
| |
Additional characters and punctuation marks can be used within the format string. Characters that match any of the formatting characters must be preceded by "\". |
Examples of applying a user-defined format string to date/time values are shown in the following table.
| Format | Output |
| Format(Now,"M/d/yy") |
8/30/10 |
| Format(Now,"M-d-yyyy") |
8-30-2010 |
| Format(Now,"d-MMMM-yy") |
30-August-10 |
| Format(Now,"d MMMM, yyyy") |
30 August, 2010 |
| Format(Now,"MMMM d, yyyy") |
August 30, 2010 |
| Format(Now,"MMMM, yyyy") |
August, 2010 |
| Format(Now,"%d") |
30 |
| Format(Now,"h:m tt") |
2:25 AM |
| Format(Now,"h:m:ss tt") |
2:25:32 AM |
| Format(Now,"H:m") |
2:25 |
| Format(Now,"M/d/yy - h:mtt") |
8/30/10 - 2:25AM |
| Format(Now,"H:m:ss.fffffff") |
2:25:32.2230423 |
| Format(Now,"To\da\y i\s MMMM d, yyyy.") |
Today is August 30, 2010.
|