Converts a numerical value to a String value based upon a specified output format specification.
...Format( expression, format_s )
Prerequisites
None
Parameters
expression
A required numeric expression. This defines the numerical value that is to be converted to a string. This value can be any numeric type, e.g. Integer, Double, Boolean, etc.
format_s
An optional String expression. This String expression defines the output format to generate. If format_s is not specified or is an empty String value, the default format ("G") is utilized.
Remarks
This function converts a numerical value to a String in a specified format. The format_s value specifies one of several pre-defined formats or defines a custom format. If the format specification is not recognized, the contents of format_s are copied to the output in place of a converted numerical value.
To specify a pre-defined formats, format_s must contain one of the single character specifications described in Table 19-99.
Predefined Formats | Output Format |
---|---|
"G" or "g" |
General purpose format. Displays a maximum of 17 characters including the sign character. Includes at least one integer digit with no leading space characters or trailing zero's in the fractional part. If the number is too large to display in 17 characters, this format automatically switches to scientific notation. |
"F" or "f" |
Fixed format. Always displays two fractional digits plus at least one integer digit and more as required. No leading or trailing space characters are generated. |
"E" or "e" |
Scientific notation. Generates a value in the form of "[s]n.nnnnnnesxx" where "s" is a "+" or "-" sign character and "xx" is the base 10 exponent. |
The custom format definition is a character by character literal description of the output format. For example, "0.00#" specifies that the output is to contain as least one integer digit and two fractional digits with an optional third fractional digit. If the numerical value contains more integer digits than specified by the format, additional digits are added to the left to fully display the numerical value. If additional fractional digits exist, the fractional part is rounded to the specified number of fractional digits and only the specified fractional digits are displayed. Leading and trailing space characters are not included in the output.
Table 19-100 defines the character placeholders permitted in a custom format.
Custom Formats | Output Format |
---|---|
"0" |
Displays a digit or "0" if none. If a "0" is to the left of the decimal point, sufficient leading zeros are generated to display the specified number of decimal digits. Likewise, a "0" to the right of the decimal point always results in a digit or a "0" character. For instance, when the number 23 is displayed using the format "0000.0", the output of the Format function is "0023.0". |
"#" |
Displays a digit or nothing. If a "#" is to the left of the decimal point, a digit is displayed if it is non-zero else nothing is added to the output stream. Likewise, if a "#" is to the right of the decimal point, only non-zero digits are displayed. For instance, when the number 23 is displayed using the format "###0.#", the output of this function is "23.". |
"." |
Decimal point placeholder. Separates integer and fractional placeholders. Also, results in a "." being included in the output stream. |
"E" or "e" |
Scientific notation. Outputs a number in scientific notation. This format always generates one digit to the left of the decimal point and a sign character and two digits in the exponent, e.g. "[s]n.nnnnesxx". The significance of the custom format is to specify the number of fractional digits to be included. |
Examples
Dim stg_a As String ' Create string variable
stg_a = Format(2323) ' Default ("G") format, "2323"
stg_a = Format(2323,"G") ' General ("G") format, "2323"
stg_a = Format(2323,"F") ' Fixed ("F") format, "2323.00"
stg_a = Format(2323,"E") ' Exponential ("E") format, "2.323000e+03"
stg_a = Format(.2,".0#") ' Outputs ".2"
stg_a = Format(.23,".0#") ' Outputs ".23"
stg_a = Format(-.23,".0#") ' Outputs "-.23"
stg_a = Format(2.1,".##") ' Outputs "2.1"
stg_a = Format(23.23,".000") ' Outputs "23.230"
stg_a = Format(23.23,"0000") ' Outputs "0023"
stg_a = Format(23.23,"0") ' Outputs "23"
stg_a = Format(-.23,"0.00e000") ' Outputs "-2.30e-01"
See Also