Siebel VB Language Reference > VB Language Reference >

Format Function


This standard VB function returns a formatted string of an expression based on a given format.

Syntax

Format[$](expression[, format])

Argument
Description

expression

The value to be formatted; it can be a number, string, or variant

format

A string expression representing the format to use

Select one of the topics that follow for a detailed description of format strings.

Returns

The expression in the specified format.

Usage

The Format function formats the expression as a number, date, time, or string depending upon the format argument. The dollar sign ($) in the function name is optional. If it is included, the return type is string. Otherwise the function returns a variant of vartype 8 (string). As with any string, you must enclose the format argument in quotation marks ("").

Numeric values are formatted as either numbers or date/times. If a numeric expression is supplied and the format argument is omitted or null, the number is converted to a string without any special formatting.

Both numeric values and variants can be formatted as dates. When formatting numeric values as dates, the value is interpreted according the standard Basic date encoding scheme. The base date, December 30, 1899, is represented as zero, and other dates are represented as the number of days from the base date.

Strings are formatted by transferring one character at a time from the input expression to the output string.

CAUTION:  The Format function does not give the correct format if the format string does not match the Regional Settings, or if the Date in the Windows setting is not set to the U.S. format.

Formatting Numbers

The predefined numeric formats with their meanings are as follows:

Format
Description

General Number

Displays the number without thousand separator.

Fixed

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

Standard

Displays the number with thousand separator and two digits to the right of decimal separator.

Scientific

Displays the number using standard scientific notation.

Currency

Displays the number using a currency symbol as defined in the International section of the Control Panel. Use thousand separator and display two digits to the right of decimal separator. Enclose negative value in parentheses.

Percent

Multiplies the number by 100 and displays it with a percent sign appended to the right; displays two digits to the right of decimal separator.

True/False

Displays FALSE for 0, TRUE for any other number.

Yes/No

Displays No for 0, Yes for any other number.

On/Off

Displays Off for 0, On for any other number.

To create a user-defined numeric format, follow these guidelines:

For a simple numeric format, use one or more digit characters and (optionally) a decimal separator. The two format digit characters provided are zero, "0", and number sign, "#". A zero forces a corresponding digit to appear in the output; while a number sign causes a digit to appear in the output if it is significant (in the middle of the number or non-zero).

Number
Format
Result

1234.56

#

1235

1234.56

#.##

1234.56

1234.56

#.#

1234.6

1234.56

######.##

1234.56

1234.56

00000.000

01234.560

0.12345

#.##

.12

0.12345

0.##

0.12

A comma placed between digit characters in a format causes a comma to be placed between every three digits to the left of the decimal separator.

Number
Format
Result

1234567.8901

#,#.##

1,234,567.89

1234567.8901

#,#.####

1,234,567.8901

NOTE:  Although a comma and period are used in the format specification to denote separators for thousands and decimals, the output string contains the appropriate character based upon the current international settings for your machine.

Numbers can be scaled either by inserting one or more commas before the decimal separator or by including a percent sign in the format specification. Each comma preceding the decimal separator (or after all digits if no decimal separator is supplied) scales (divides) the number by 1000. The commas do not appear in the output string. The percent sign causes the number to be multiplied by 100. The percent sign appears in the output string in the same position as it appears in format.

Number
Format
Result

1234567.8901

#,.##

1234.57

1234567.8901

#,,.####

1.2346

1234567.8901

#,#,.##

1,234.57

0.1234

#0.00%

12.34%

Characters can be inserted into the output string by being included in the format specification. The following characters are inserted in the output string in a location matching their position in the format specification:

        -  + $ ( space

Any set of characters can be inserted by enclosing them in double quotes. Any single character can be inserted by preceding it with a backslash, \.

Number
Format
Result

1234567.89

$#,0.00

$1,234,567.89

1234567.89

"TOTAL:" $#,#.00

TOTAL: $1,234,567.89

1234

\ = \>#,#\<\ =

= >1,234< =

You can use the standard VB Chr function if you need to embed quotation marks in a format specification. The character code for a quotation mark is 34.

Numbers can be formatted in scientific notation by including one of the following exponent strings in the format specification:

     E- E +  e- e +

Precede the exponent string with one or more digit characters. The number of digit characters following the exponent string determines the number of exponent digits in the output. Format specifications containing an uppercase E result in an uppercase E in the output. Those containing a lowercase e result in a lowercase e in the output. A minus sign following the E causes negative exponents in the output to be preceded by a minus sign. A plus sign in the format causes a sign to always precede the exponent in the output.

Number
Format
Result

1234567.89

###.##E-00

123.46E04

1234567.89

###.##e + #

123.46e + 4

0.12345

0.00E-00

1.23E-01

A numeric format can have up to four sections, separated by semicolons. If you use only one section, it applies to every value. If you use two sections, the first section applies to positive values and zeros, the second to negative values. If you use three sections, the first applies to positive values, the second to negative values, and the third to zeros. If you include semicolons with nothing between them, the undefined section is printed using the format of the first section. The fourth section applies to Null values. If it is omitted and the input expression results in a NULL value, Format returns an empty string.

Number
Format
Result

1234567.89

#,0.00;(#,0.00);"Zero";"NA"

1,234,567.89

-1234567.89

#,0.00;(#,0.00);"Zero";"NA"

(1,234,567.89)

0.0

#,0.00;(#,0.00);"Zero";"NA#"

Zero

0.0

#,0.00;(#,0.00);;"NA"

0.00

Null

#,0.00;(#,0.00);"Zero";"NA"

NA

Null

"The value is: "

0.00

Formatting Dates and Times

As with numeric formats, there are several predefined formats for formatting dates and times:

Format
Description

General Date

If the number has both integer and real parts, displays both date and time (for example, 11/8/93 1:23:45 PM); if the number has only an integer part, displays it as a date; if the number has only a fractional part, displays it as time

Long Date

Displays a Long Date; Long Date is defined in the International section of the Control Panel

Medium Date

Displays the date using the month abbreviation, without the day of the week (for example, 08-Nov-93)

Short Date

Displays a Short Date; Short Date is defined in the International section of the Control Panel

Long Time

Displays a Long Time; Long Time is defined in the International section of the Control Panel and includes hours, minutes, and seconds

Medium Time

Does not display seconds; displays hours in 12-hour format and uses the AM/PM designator

Short Time

Does not display seconds; uses 24-hour format and no AM/PM designator

In a user-defined format for a date, the format specification contains a series of tokens. Each token is replaced in the output string by its appropriate value.

A date can be output by using a combination of the following tokens:

Token
Output

c

The equivalent of the format ddddd ttttt. Read the definitions that follow.

ddddd

The date including the day, month, and year according to the machine's current Short Date setting; the default Short Date setting for the United States is m/d/yy.

dddddd

The date including the day, month, and year according to the machine's current Long Date setting; the default Long Date setting for the United States is mmmm dd, yyyy.

ttttt

The time including the hour, minute, and second using the machine's current time settings; the default time format is h:mm:ss AM/PM.

Finer control over the output is available by including format tokens that deal with the individual components of the date-time. These tokens are:

Token
Output

d

The day of the month as a one or two digit number (1-31)

dd

The day of the month as a two digit number (01-31)

ddd

The day of the week as a three letter abbreviation (Sun-Sat)

dddd

The day of the week without abbreviation (Sunday-Saturday)

w

The day of the week as a number (Sunday as 1, Saturday as 7)

ww

The week of the year as a number (1-53)

m

The month of the year or the minute of the hour as a one or two digit number. The minute is output if the preceding token was an hour; otherwise, the month is output.

mm

The month or the year or the minute of the hour as a two digit number. The minute is output if the preceding token was an hour; otherwise, the month is output.

mmm

The month of the year as a three letter abbreviation (Jan-Dec)

mmmm

The month of the year without abbreviation (January-December)

q

The quarter of the year as a number (1-4)

y

The day of the year as a number (1-366)

yy

The year as a two-digit number (00-99)

yyyy

The year as a three- or four-digit number (100-9999)

h

The hour as a one- or two-digit number (0-23)

hh

The hour as a two-digit number (00-23)

n

The minute as a one- or two-digit number (0-59)

nn

The minute as a two-digit number (00-59)

s

The second as a one- or two-digit number (0-59)

ss

The second as a two-digit number (00-59)

By default, times display using a military (24-hour) format. Several tokens are provided in date time format specifications to change this default. They use a 12-hour format. These are:

Token
Output

AM/PM

An uppercase AM with any hour before noon; an uppercase PM with any hour between noon and 11:59 PM

am/pm

A lowercase am with any hour before noon; a lowercase pm with any hour between noon and 11:59 PM

A/P

An uppercase A with any hour before noon; an uppercase P with any hour between noon and 11:59 PM

a/p

A lowercase a with any hour before noon; a lowercase p with any hour between noon and 11:59 PM

AMPM

The contents of the 1159 string (s1159) in the WIN.INI file with any hour before noon; the contents of the 2359 string (s2359) with any hour between noon and 11:59 PM. Note: ampm is equivalent to AMPM.

Any set of characters can be inserted into the output by enclosing them in double quotes. Any single character can be inserted by preceding it with a backslash, "\".

Formatting Strings

By default, string formatting transfers characters from left to right. The exclamation point, !, when added to the format specification causes characters to be transferred from right to left. By default, characters being transferred are not modified. The less than, <, and the greater than, >, characters force case conversion on the transferred characters. Less than forces output characters to be in lowercase. Greater than forces output characters to be in uppercase.

Character transfer is controlled by the at sign, @, and the ampersand, &, characters in the format specification. These operate as follows:

Character
Interpretation

@

Output a character or a space; if there is a character in the string being formatted in the position where the @ appears in the format string, display it; otherwise, display a space in that position.

&

Output a character or nothing; if there is a character in the string being formatted in the position where the & appears, display it; otherwise, display nothing.

A format specification for strings can have one or two sections separated by a semicolon. If you use one section, the format applies to all string data. If you use two sections, the first section applies to string data, the second to Null values and zero-length strings.

Examples

This example demonstrates some of the string-formatting tokens.

Sub Button1_Click
   Dim msgtext As String
   msgtext = Format("Section #AB-234", "<\[&&&&&&&&&&&&&&&\]") _
   & Chr$(13) & Chr$(13) & Format("incoming", ">@@@@@@@@\!\!") _
   & Chr$(13) & Chr$(13) _
   & Format("Profits are expected to rise.", _
   "!&&&&&&&&&&&&&&&&&")
End Sub

This example calculates the square root of 2 as a double-precision floating point value and displays it in scientific notation.

Sub Button1_Click
   Dim value As Double
   Dim msgtext As String
   value = CDbl(Sqr(2))
   msgtext = "The square root of 2 is " & Format(value, "Scientific")
End Sub

This example uses several different date-formatting tokens to format the result of the Now function, which returns the current date and time on the computer's clock.

Sub ClickMe_Click
   dim msgtext As String
   msgtext = Now & Chr$(13) & Chr$(13) _
   & "Today is " & Format(Now, "dddd") & ", " _
   & Format(Now, "mmmm") & " " & Format(Now, "dd") & ", " _
   & Format(Now, "yyyy") & "." _
   & Chr$(13) & "The time is " & Format(Now, "h:nn am/pm") _
   & " and " & Format(Now, "s") & " seconds."
End Sub

For other examples of the Format function, read CCur Function, FV Function, and GoTo Statement.

See Also

Asc Function
CCur Function
CDbl Function
Chr Function
CInt Function
CLng Function
CSng Function
CStr Function
CVar Function
CVDate Function
Str Function

Siebel VB Language Reference