Programming Gambas from Zip/Formatting

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Formatting[edit]

These examples may vary depending on the regional settings on your computer (for time, date and currency).

Formatting Numbers[edit]

Example: Label1.text = Format(123456.789, ",#.00") shows as 123,456.79

Format(123456.789) shows as if you used gb.Standard

http://Gambaswiki.org/wiki/cat/constant

Formatting Constants[edit]

gb.Standard Uses gb.GeneralNumber for formatting numbers and gb.GeneralDate for formatting dates and times.
gb.GeneralNumber Writes a number with twelve decimal digits. Uses scientific format if its absolute value is lower than 10-4 (0.0001) or greater than 107 (1 million).
gb.Fixed Equivalent to "0.00"
gb.Percent Equivalent to "###%"
gb.Scientific Write a number with its exponent (power of ten) and eighteen decimal digits.

Symbols in the Format Strings[edit]

Symbols other than these print as they appear. For example, $ prints as is.

+ Print the sign of the number. Format(Pi, "+#.###") +3.142
- Print the sign of the number only if it is negative. Format(Pi, "-#.###") 3.142
# Print a digit only if necessary.

One # before the decimal point is all that is needed. After the decimal point, as many #’s as you want decimal places.

Format(123.456789, "#.###") 123.457
0 Always print a digit, padding with a zero if necessary. Format(24.5, "$#.00") $24.50
. Print the decimal point Format(123.456, "#.0") 123.5
, Separate the thousands Format(1234567890, "#,")

Format(1234567890, ",#")

1,234,567,890
% Multiply the number by 100 and print a percent sign. Format(0.25, "#%") 25%
E This is Scientific Notation, which is

“Something-times-ten-to-the-power-of-something”.

“E” means “times ten to the power of...”

1.2E+3 means "Start with 1.200, then move the decimal point three places to the right (get bigger x 1000) to become 1200."

Negative numbers after the “E” mean move the decimal point to the left.

Format(1234.5, "#.#E##")

Format(0.1234, "#.#E##")

1.2E+3

1.2E-1

$ The national currency symbol (according to the country as set on your computer) Format(-1234.56, "$,#.###") -$1,234.56
$$ The international currency symbol (according to the country as set on your computer) Format(-1234.56, "$$,#.###") -AUD 1,234.56
( ) Negative numbers represented with brackets, which is what finance people use. Format(-123.4, "($$,#.00)") (AUD 123.40)

Formatting Dates[edit]

Example: Format(Now, gb.Standard) shows as 10/07/2019 21:07:26

Formatting Constants[edit]

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. Writes nothing for a null date or a short time when there is no date, and writes the date and time for all other cases. Format(Now, gb.GeneralDate) is 10/07/2019 21:17:45
gb.Standard Uses gb.GeneralNumber for formatting numbers and gb.GeneralDate for formatting dates and times. 10/07/2019 21:20:45
gb.LongDate Long date format Wednesday 10 July 2019
gb.MediumDate Medium date format 10 Jul 2019
gb.ShortDate Short date format 10/07/2019
gb.LongTime Long time format 21:22:35
gb.MediumTime Medium time format 09:23 PM
gb.ShortTime Short time format 21:23

Format String Symbols[edit]

Label1.text = Format(Now, "dddd dd/mm/yyyy hh:nn:ss") shows as Tuesday 09/07/2019 20:45:13

yy The year in two digits h The hour
yyyy The year in four digits hh The hour in two digits.
m The month n The minutes.
mm The month in two digits. nn The minutes in two digits
mmm Abbreviated month s The seconds
mmmm Full name of the month ss The seconds in two digits
d The day : The time separator
dd The day in two digits u A point and the milliseconds, if non-zero
ddd Abbreviated weekday uu A point and the milliseconds in three digits.
dddd Full name of the weekday t The timezone alphabetic abbreviation
/ The date separator tt The timezone in HHMM format
AM/PM The AM or PM symbol

Formatting Currency[edit]

For the symbols in a format string, see above (numbers).

gb.Currency Uses the national currency symbol. Format(14.50, gb.Currency)

shows as $ 14.5

gb.International Uses the international currency symbol. Format(14.50, gb.International)

shows as AUD 14.5

Programming Gambas from Zip
 ← DataTypes Formatting OperatorPrecedence →