Home / lang / format 
Format$
Syntax
String = Format$ ( Expression [ , Format ] )
String = Format ( Expression [ , Format ] )

Converts the Expression to a String by using a format that depends on the type of the Expression .

This function can format dates, times, numbers, and currencies.

Format can be a predefined format (an Integer constant) or a user-defined format (a String that depicts the format) or can be omitted.

See Predefined Constants for a list of predefined formats.

If Format is not specified, gb.Standard is used.

This function uses localization information.

User-defined formats for a numeric expression

A user-defined format is described by a sequence of special characters.

Arbitrary characters specified before and after the format strings will be printed as is

Characters for user-defined formats

+ Prints the sign of the number.
- Prints the sign of the number only if it is negative.
# Prints a digit only if necessary.
0 Always prints a digit, padding with a zero if necessary.
. Prints the decimal separator.
, Prints the thousand separators.
% Multiplies the number by 100 and prints a per-cent sign.
E Introduces the exponential part of a Float number. The sign of the exponent is always printed.

Example
PRINT Format$(Pi, "-#.###")

3.142
Example
PRINT Format$(Pi, "+0#.###0")

+03.1416
Example
PRINT Format$(Pi / 10, "###.# %")

31.4 %
Example
PRINT Format$(-11 ^ 11, "#.##E##")

-2.85E+11

User-defined format characters for currencies

$ Prints the national currency symbol.
$$ When the $ is doubled, the international currency symbol is printed instead.
( Prints the representation of negative currencies. This must be the first character of the format. You can specify a closed brace ) at the end of the format.

Example
PRINT Format$(1972.06, "$#.###")

$1972.06
Example
PRINT Format$(-1972.06, "$,#.###")

-$1,972.06
Example
PRINT Format$(-1972.06, "($,#.###)")

$1,972.06 or 1.972,06 €

User-defined format characters for dates

yy Prints the year on two digits.
yyyy Prints the year on four digits.
m Prints the month.
mm Prints the month on two digits.
mmm Prints the month in an abbreviatted string form.
mmmm Prints the month in its full string form.
d Prints the day.
dd Prints the day on two digits.
ddd Prints the week day in an abbreviated form.
dddd Prints the week day in its full form.
/ Prints the date separator.
h Prints the hour.
hh Prints the hour on two digits.
n Prints the minutes.
nn Prints the minutes on two digits.
s Prints the seconds.
ss Prints the seconds on two digits.
: Prints the time separator.
u Prints the milliseconds, if they are different from zero.

Example
PRINT Format$(Now, "mm/dd/yyyy hh:nn:ssu")

04/15/2002 09:05:36.26
Example
PRINT Format$(Now, "m/d/yy h:n:s")

4/15/02 9:5:36
Example
PRINT Format$(Now, "ddd dd mmm yyyy")

Mon 15 Apr 2002
Example
PRINT Format$(Now, "dddd dd mmmm yyyy")

Monday 15 April 2002
Example
PRINT Format$(Now, "yyyy-mm-dd hh-nn-ss")
PRINT Format$(Now, "yyyy/mm/dd hh:nn:ss")

2006-04-29 07-41-11 2006.04.29 07:41:11

See also
Formatting functions