Difference between revisions of "Gambas/functions/Format"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
Jump to navigation Jump to search
m (Woozle moved page Format (Gambas) to Gambas/reference/Format: reorganizing)
m (relevant internal link)
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Gambas article|Format}}{{boxright|__TOC__}}
+
{{boxright|__TOC__}}
 
==Usage==
 
==Usage==
  ''String'' = [[Format (Gambas)|Format]]$ ( ''Expression'' [ , ''Format'' ] )
+
  ''String'' = {{l/same|Format}}$ ( ''Expression'' [ , ''Format'' ] )
  ''String'' = [[Format (Gambas)|Format]] ( ''Expression'' [ , ''Format'' ] )
+
  ''String'' = {{l/same|Format}} ( ''Expression'' [ , ''Format'' ] )
 
* '''See also''':
 
* '''See also''':
** ''String'' = [[CStr (Gambas)|CStr]](''expression'') - converts ''expression'' to a ''string'' in the default format
+
** ''String'' = {{l/same|CStr}}(''expression'') - [[Gambas/syntax/variables/type conversion|converts]] ''expression'' to a ''string'' in the default format
 
==Action==
 
==Action==
 
Converts an ''expression'' to a ''string'' based on the given ''format''. The format may be either user-defined or one of several pre-defined formats. If ''format'' is not specified, '''gb.Standard''' is used (see [[#general]]).
 
Converts an ''expression'' to a ''string'' based on the given ''format''. The format may be either user-defined or one of several pre-defined formats. If ''format'' is not specified, '''gb.Standard''' is used (see [[#general]]).

Latest revision as of 11:42, 14 May 2015

Usage

String = Format$ ( Expression [ , Format ] )
String = Format ( Expression [ , Format ] )
  • See also:
    • String = CStr(expression) - converts expression to a string in the default format

Action

Converts an expression to a string based on the given format. The format may be either user-defined or one of several pre-defined formats. If format is not specified, gb.Standard is used (see #general).

  • Localization settings may affect the output of this function.

User-Defined Formats

numeric

+ 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.

Examples

PRINT Format$(Pi, "-#.###") 3.142
PRINT Format$(Pi, "+0#.###0") +03.1416
PRINT Format$(Pi / 10, "###.# %") 31.4 %
PRINT Format$(-11 ^ 11, "#.##E##") -2.85E+11

currency

$ Prints the national currency symbol.
$$ When the $ is doubled, the international currency symbol is printed instead.
( f ) Designates the representation of negative currencies, where f is the format specifier

Examples

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

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.

Examples

PRINT Format$(Now, "mm/dd/yyyy hh:nn:ss") 04/15/2002 09:05:36
PRINT Format$(Now, "m/d/yy h:n:s") 4/15/02 9:5:36
PRINT Format$(Now, "ddd dd mmm yyyy") Mon Apr 15 2002
PRINT Format$(Now, "dddd dd mmmm yyyy") Monday April 15 2002

Pre-Defined Formats

Numeric

gb.GeneralNumber Use twelve decimal digits, or scientific format if the absolute value is lower than 10Template:exp or greater than 10Template:exp.
gb.Fixed Equivalent to "0.00"
gb.Percent Equivalent to "###%"
gb.Scientific Use an exponent and eighteen decimal digits.

date and time

gb.GeneralDate Write a date only if the date and time value has a date part, and write a time only if it has a date part.
gb.LongDate Long date format.
gb.MediumDate Medium date format.
gb.ShortDate Short date format.
gb.LongTime Long time format.
gb.MediumTime Medium time format.
gb.ShortTime Short time format.

currency

gb.Currency Format a currency by using the national currency symbol.
gb.International Format a currency by using the international currency symbol.

general

Use gb.GeneralNumber for formatting numbers and gb.GeneralDate for formatting dates and times.

Source

  • Much of this text has been copied from here, which is presumed to be available under a GNU-compatible license.