Home > Language Reference > Functions > Strings
Format Function
Returns a string containing a numeric expression or a date formatted according to the instructions contained in a formatting expression.
Syntax
Public Function Format(ByVal vValue As Double, ByRef sFormat As String, Optional ByVal bLocale As Boolean = True) As String
Public Function Format(ByVal vValue As Date, ByRef sFormat As String, Optional ByVal bLocale As Boolean = True) As String
| Parameter | Description |
| vValue | Date or numeric value to format. |
| sFormat | Formatting expression. |
| bLocale | Indicates if the user defined date and hour formats should be used. |
Remarks
The Format function only handles numbers and dates. This function cannot format other types of data. If the vValue parameter does is not a Byte, Integer, Long, Single, Double or Date, a compile time error will occur.
The sFormat parameter defines the format of the string returned. It can contain either a predefined format name or a user defined format. The syntax of this parameter is explained later on. If you use a date format to format a number, or if you use a number format to format a date, a runtime error will occur.
The optional bLocale parameter indicates if the function should take into account the local settings when formatting. If the parameter is False, the numbers are formatted without thousands separators, and the decimal separators will be decimal point. Dates will be formated using the '/' character as a date separator, and the ':' character as the hour separator. If this parameter is True, the separators used are those defined in the system preferences.
Named numeric formats
The following table identifies the predefined numeric format names:
| Format name | Description |
| Currency | Displays two figures after the decimal separator. |
| Integer | Does not display the decimal part of the number. |
| Scientific | Uses standard scientific notation with 3 digits after the decimal separator. The exposant is displayed with a capital E. |
| Engineer | Uses standard scientific notation with 3 digits after the decimal separator, rounding the exposant down to 3. The exposant is displayed with a capital E. |
For example, on a device configured to use US number formats :
Format(12345.987,"currency") ' Returns "12,346.99" Format(12345.987,"integer") ' Returns "12,346" Format(12345.987,"scientific") ' Returns "1.235E4" Format(12345.987,"engineer") ' Returns "12.346E3"
User defined numeric formats
A user defined number format should use the following syntax::
[ + ] num [ . num ] [ { e | E | g | G } [ + ] num ]
The table below explain the meaning of the different elements :
| Specifier | Display obtained |
| + | Forces display of the sign. |
| . | Displays the decimal separator. |
| e | Displays the number in scientific notation using a lowercase "e". |
| E | Displays the number in scientific notation using a capital "E". |
| g | Displays the number in scientific notation using a lowercase "e". The exposant is rounded down to 3. |
| G | Displays the number in scientific notation using a capital "E". The exposant is rounded down to 3. |
The num format specifier is made up of a number of zero's or '#' indicating the number of characters to display, for example "00" ou "###". The zero character indicates a compulsory digit, whilst the # character indicates an optional digit.
For example, on a device configured to use US number formats :
Format(12345.987,"0.0") ' Returns "12,346.0" Format(12345.987,"0.00") ' Returns "12,345.99" Format(12345.987,"0.0000") ' Returns "12,345.9870" Format(12345.987,"0.####") ' Returns "12,345.987" Format(12345.987,"+000000") ' Returns "+012,346" Format(12345.987,"#.##E+00") ' Returns "1.23E+04" Format(12345.987,"#.##g+0") ' Returns "12.35e+3"
Named Date / Time formats
The following table identifies the predefined date and time format names:
| Format name | Description |
| shortdate | Displays the date, in a shortened format, taking system preferences into account |
| longdate | Displays the date, in full, taking system preferences into account |
| iso | Displays the date according to the ISO 8601 format |
For example, on a device configured to use US number formats :
Format(Now,"shortdate") ' Returns "12/31/99" Format(Now,"longdate") ' Returns "Dec 31 1999" Format(Now,"iso") ' Returns "1999-12-31T21:45:03"
User defined Date / Time formats
The format string can contain one or more of the following elements, in any order :
| Specifier | Description |
| : | Displays the hour separator defined in the system preferences by the user |
| / | Displays the date separator defined in the system preferences by the user |
| [espace] | Displays a space |
| yyyy | Displays a four digit year (1904 - 2031) |
| yy | Displays the last two digits of a year (00 - 99) |
| m | Displays the month number (1 - 12) |
| mm | Displays the month number in two digits (01 - 12) |
| mmm | Displays the abbreviated month name |
| mmmm | Displays the full month name |
| d | Displays the day of the month (1 - 31) |
| dd | Displays the day of the month in two digits (01 - 31) |
| ddd | Displays the abbreviated day of the week name |
| dddd | Displays the full day of the week name |
| h | Displays the hour (1 - 23) |
| hh | Displays the hour in two digits (01 - 23) |
| n | Displays the minute (1 - 59) |
| nn | Displays the minute in two digits (01 - 59) |
| s | Displays the second(1 - 59) |
| ss | Displays the second in two digits (01 - 59) |
| am/pm | Uses a 12 hour display for hours, and shows "am" for hours in the morning and "pm" for hours after midday. |
| AM/PM | Uses a 12 hour display for hours, and shows "AM" for hours in the morning and "PM" for hours after midday. |
| a/p | Uses a 12 hour display for hours, and shows "a" for hours in the morning and "p" for hours after midday. |
| A/P | Uses a 12 hour display for hours, and shows "A" for hours in the morning and "P" for hours after midday. |
For example, on a device configured to use US number formats :
Format(Now,"hh:nn:ss") ' Returns "23:59:59" Format(Now,"hh:nn:ss am/pm") ' Returns "11:59:59 pm" Format(Now,"mmmm dd yyyy") ' Returns "December 31 1999" Format(Now,"mm/dd/yyyy") ' Returns "12/31/1999"
The Format function comes with 2 flavors, one for each data type it can handle. The compiler determines at compile time which version to call depending on the type of vValue. For more information about this mechanism, please refer to the Polymorphic functions topic.
System requirements
| System | Minimal version | Remarks |
| Palm OS | Palm OS 3.0 | N/A |