Q(uick)BASIC Function: FormatX$
Quick View
FormatX$
These functions provide a wide range of formats for numeric and date/time data
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- FormatI$ (expression# , fmt$)
- FormatL$ (expression# , fmt$)
- FormatS$ (expression# , fmt$)
- FormatD$ (expression# , fmt$)
- FormatC$ (expression# , fmt$)
Description/Parameter(s)
expression# | A numeric expression to be formatted. |
fmt$ | A string expression that consists of BASIC display-format characters detailing how the expression is to be displayed. |
Returns | A string that contains the formatted expression. |
- If the argument fmt$ is not specified for a null string, the general format - that which would give output identical to output with no formatting - is used.
Usage Notes
- To use any of the FormatX$ functions, you must first start BASIC with the correct Quick library, link your program with FORMAT.LIB, or use the FORMAT.QLB Quick library.
- The following symbols are used to create formats:
This symbol | Means this |
Null string | Display the number in general format. |
0 | Digit place holder: |
|
|
# | Digit place holder: |
|
|
. | Decimal point: |
|
|
% | Percentage |
|
|
, | Thousands separator: |
|
|
E- E+ e- e+ | Scientific format: |
|
|
: - + $ () space | Display that literal character: |
|
|
Display the next character in the format string: | |
|
|
"abc" | Display whatever text is inside the double quotes: |
|
- Some sample numeric formats are shown below (these examples all assume the country code is set to 1, the United States).
Result for: | |||
Format (fmt$) | 5 | -5 | .5 |
Null string | 5 | -5 | 0.5 |
0 | 5 | -5 | 1 |
0.00 | 5.00 | -5.00 | 0.50 |
#,##0 | 5 | -5 | 1 |
#,##0.00 | 5.00 | -5.00 | 0.50 |
$#,##0;($#,##0) | $5 | ($5) | $1 |
$#,##0.00;($#,##0.00) | $5.00 | ($5.00) | $0.50 |
0% | 500% | -500% | 50% |
0.00% | 500.00% | -500.00% | 50.00% |
0.00E+00 | 5.00E+00 | -5.00E+00 | 5.00E-01 |
0.00E-00 | 5.00E00 | -5.00E00 | 5.00E-01 |
- A number format can have three sections separated by semicolons. The first section formats positive values; the second section formats negative values, and the third section formats zeros. The following example has two sections: the first section defines the format for numbers greater than 0; the second section defines the format for numbers less than 0:
- $#,##0;($#,##0)
- You can use from one to three sections:
If you use | The result is |
One section only | The format applies to all numbers. |
Two sections | The first section applies to positive numbers and zeros; the second to negative numbers. |
Three sections | The first section applies to positive numbers, the second to negative numbers numbers, and the third to zeros. |
- If you have semicolons with nothing between them, the missing section prevents that type of number from being displayed. For example, the following format will print positive numbers and zeros, but it will not print negative numbers.
- $#,##0;;"Zero"
Date/Time Formats
- Date/time serial numbers can be formatted with date/time or with numeric formats (since date/time serial numbers are stored as floating-point values). Date/time formats are:
This symbol | Means this |
d dd ddd dddd | Display the day as a number without leading zeros (1-12), as a number with leading zeros (01-12), as an abbreviation (Sun-Sat), or as a full name (Sunday-Saturday). |
m mm mmm mmmm | Display the month as a number without leading zeros (1-12), as a number with leading zeros (01-12), as an abbreviation (Jan-Dec), or as a full month name (January-December). If you use m or mm immediately after the h or hh symbol, the minute rather than the month is displayed. |
yy yyyy | Display the year as a two-digit number (00-99), or as a four-digit number (1900-2040). |
h hh | Display the hour as a number without leading zeros (0-23), or as a number with leading zeros (00-23). If the format contains an AM or PM, the hour is based on the 12-hour clock. Otherwise, the hour is based on the 24-hour clock. |
m mm | Display the minute as a number without leading zeros (0-59), or as a number with leading zeros (00-59). The m or mm must appear after an h or hh, or the month is displayed rather than the minute. |
s ss | Display the second as a number without leading zeros (0-59), or as a number with leading zeros (00-59). |
AM/PM am/pm A/P a/p |
Display the hour using a 12-hour clock. AM, am, A, or a are displayed for times up until noon; PM, pm, P, or p are displayed for times between noon and midnight. |
- The following are examples of date and time formats:
Format | Display |
m/d/yy | 12/7/58 |
d-mmmm-yy | 7-December-58 |
d-mmmm | 7-December |
mmmm-yy | December-58 |
h:mm AM/PM | 8:50 PM |
h:mm:ss AM/PM | 8:50:35 PM |
h:mm | 20:50 |
h:mm:ss | 20:50:35 |
m/d/yy h:mm | 12/7/58 20:50 |
Example
This example uses the FormatD$ function to format date and time information. The current date and time are returned as a serial number by the Now# function.
Note: To run this example you must use a Quick library that includes the procedures contained in the date/time/format library files. The following include files must also be present.
'$INCLUDE: 'DATIM.BI'
'$INCLUDE: 'FORMAT.BI'
CLS
PRINT "Today's date is "; FormatD$(Now#, "dd-mmm-yy"); "."
PRINT "The correct time is "; FormatD$(Now#, "hh:mm:ss AM/PM"); "."
Sample Output:
Today's date is 02-May-1989. The correct time is 2:22:14 PM.See also: