Format Function

Converts a numeric expression to a string, and then formats it according to the format that you specify.

Syntax:


Format(expression [, format As String]) As String

Parameters:

expression: Numeric expression that you want to convert to a formatted string.

format: String that specifies the format code for the number. If format is omitted, the Format function works like the LibreOffice Basic Str() function.

Return type:

Text string.

Formatting Codes

In BASIC, a format code can be divided into three sections that are separated by semicolons. The first part defines the format for positive values, the second part for negative values, and the third part for zero. If you only specify one format code, it applies to all numbers.

You can set the locale used for controlling the formatting numbers, dates and currencies in LibreOffice Basic in - Languages and Locales - General. In Basic format codes, the decimal point (.) is always used as placeholder for the decimal separator defined in your locale and will be replaced by the corresponding character.

The same applies to the locale settings for date, time and currency formats. The Basic format code will be interpreted and displayed according to your locale setting.

The following list describes the codes that you can use for formatting a numeric expression:

Code

Description

0

If expression has a digit at the position of the 0 in the format code, the digit is displayed, otherwise a zero is displayed.

If expression has fewer digits than the number of zeros in the format code, (on either side of the decimal), leading or trailing zeros are displayed. If the expression has more digits to the left of the decimal separator than the amount of zeros in the format code, the additional digits are displayed without formatting.

Decimal places in the expression are rounded according to the number of zeros that appear after the decimal separator in the format code.

#

If expression contains a digit at the position of the # placeholder in the format code, the digit is displayed, otherwise nothing is displayed at this position.

This symbol works like the 0, except that leading or trailing zeroes are not displayed if there are more # characters in the format code than digits in the expression. Only the relevant digits of the expression are displayed.

. (period)

The decimal placeholder determines the number of decimal places to the left and right of the decimal separator.

If the format code contains only # placeholders to the left of this symbol, numbers less than 1 begin with a decimal separator. To always display a leading zero with fractional numbers, use 0 as a placeholder for the first digit to the left of the decimal separator.

The use of a period as a thousands and decimal separator is dependent on the regional setting. When you enter a number directly in Basic source code, always use a period as decimal delimiter. The actual character displayed as a decimal separator depends on the number format in your system settings.

%

Multiplies the expressionby 100 and inserts the percent sign (%) where the expression appears in the format code.

E- E+ e- e+

If the format code contains at least one digit placeholder (0 or #) to the right of the symbol E-, E+, e-, or e+, the expression is formatted in the scientific or exponential format. The letter E or e is inserted between the number and the exponent. The number of placeholders for digits to the right of the symbol determines the number of digits in the exponent.

If the exponent is negative, a minus sign is displayed directly before an exponent with E-, E+, e-, e+. If the exponent is positive, a plus sign is only displayed before exponents with E+ or e+.

- + $ ( ) space

: A plus (+), minus (-), dollar ($), space, or brackets entered directly in the format code is displayed as a literal character.

\

To display characters other than the ones listed here, you must precede it by a backslash (\), or enclose it in quotation marks (" ").

The backslash displays the next character in the format code.

Characters in the format code that have a special meaning can only be displayed as literal characters if they are preceded by a backslash. The backslash itself is not displayed, unless you enter a double backslash (\\) in the format code.

Characters that must be preceded by a backslash in the format code in order to be displayed as literal characters are date- and time-formatting characters (a, c, d, h, m, n, p, q, s, t, w, y, /, :), numeric-formatting characters (#, 0, %, E, e, comma, period), and string-formatting characters (@, &, <, >, !).


Predefined formats

You can also use the following predefined number formats. Except for "General Number", all of the predefined format codes return the number as a decimal number with two decimal places.

If you use predefined formats, the name of the format must be enclosed in quotation marks.

Code

Description

"<"

Convert expression to lower case

">"

Convert expression to upper case.

"c" or "General Date"

Returns the numeric expression in short date format, optionally with "H:MM:SS AM/PM". If expression is a string, returns the string.

"n"

Returns the minute of the numeric expression, with 1 or 2 digits.

"nn"

Returns the minute of the numeric expression with two digits.

"w"

Returns the week day of the numeric expression. 1 is Sunday and 7 is Saturday.

"General Number"

Returns the numeric expression with 12 digits (0.############).

"Currency"

Returns the numeric expression in the currency of the locale.

"Fixed"

Returns the numeric expression with 2 decimal places (0.00).

"Standard"

Returns the numeric expression with thousands separators and 2 decimals (@0.00).

"Percent"

Returns the numeric expression as percent value (0.00%).

"Scientific"

Returns the numeric expression in scientific notation (#.00E+00);

"Yes/No"

Returns "Yes" if the numeric expression is not equal to zero, "No" otherwise. "Yes" and "No" are localized.

"True/False"

Returns "True" if the numeric expression is not equal to zero, "False" otherwise. "True" and "False" are localized.

"On/Off"

Returns "On" if the numeric expression is not equal to zero, "Off" otherwise. "On" and "Off" are localized.

"Long Date" or "dddddd"

Returns the numeric expression in system long date format, and depends on the locale.

"Medium Date"

Returns the numeric expression in date format DD-MMM-YY, and depends on the locale.

"Short Date" or "ddddd"

Returns the numeric expression in system short date format, and depends on the locale.

"Long Time" or "ttttt"

Returns the numeric expression in system long time format, and depends on the locale("H:MM:SS AM/PM").

"Medium Time"

Returns the numeric expression in system medium time format, and depends on the locale (HH:MM AM/PM)

"Short Time"

Returns the numeric expression in system short time format, and depends on the locale (HH:MM).


Error codes:

5 Invalid procedure call

Example:


Sub ExampleFormat
    MsgBox Format(6328.2, "##,##0.00")
    ' always use a period as decimal delimiter when you enter numbers in Basic source code.
    ' displays for example 6,328.20 in English locale, 6.328,20 in German locale.
msgbox Format("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", "<") ' returns abcdefghijklmnopqrstuvwxyz1234567890, digits not affected.
msgbox Format("abcdefghijklmnopqrstuvwxyz1234567890", ">") ' returns ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890, digits not affected.
msgBox Format(12345.6,"c") ' returns the number in date format as 18/10/33 2:24:00 PM in German locale.
msgBox Format(12345.6,"General Date") ' Same as "c".
msgbox format(12345.004,"n") ' returns 5
msgbox format(12345.004,"nn") ' returns 05
msgbox format(12345.6,"w") ' returns 4 (Wednesday).
msgbox format(log(123),"General Number") ' returns 4,812184355372 in German locale
msgbox format(123456.7890,"Fixed") ' 123456.79
msgbox format(123456.1234,"Fixed") ' 123456.12
msgbox format(123456.7890,"Standard") '123,456.79
msgbox format(123456.1234,"Standard") '123,456.12
msgbox format(12.3456,"Percent") ' 1234.56%
msgbox format(0.123456,"Percent") '12.35%
msgbox format(123,"Yes/No") ' returns localized "Yes"
msgbox format(0,"Yes/No") ' returns localized "No"
msgbox format(-1,"True/False") ' returns localized "True"
msgbox format(123,"On/Off") ' returns localized "On"
msgbox format(45756.73,"Long Date") ' returns Wednesday, April 9, 2025 in your locale
End Sub

Please support us!