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 value:

Text string.

āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ āĻ•ā§‹āĻĄ

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

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.

.: āĻĻāĻļāĻŽāĻŋāĻ• āĻ¸ā§āĻĨāĻžāĻ¨āĻ§āĻžāĻ°āĻ•āĻŸāĻŋ āĻĻāĻļāĻŽāĻŋāĻ• āĻŦāĻŋāĻ­āĻžāĻœāĻ•ā§‡āĻ° āĻŦāĻžāĻŽ āĻ…āĻĨāĻŦāĻž āĻĄāĻžāĻ¨ā§‡āĻ° āĻĻāĻļāĻŽāĻŋāĻ• āĻ¸ā§āĻĨāĻžāĻ¨ āĻ¨āĻŋāĻ°ā§āĻ§āĻžāĻ°āĻŖ āĻ•āĻ°ā§‡ āĻĨāĻžāĻ•ā§‡āĨ¤

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.

%: 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.

āĻ¯āĻĻāĻŋ āĻāĻ•āĻŸāĻŋ āĻ¸ā§‚āĻšāĻ• āĻ‹āĻ¨āĻžāĻ¤ā§āĻŦāĻ• āĻšā§Ÿ āĻ¤āĻžāĻšāĻ˛ā§‡ E-, E+, e-, e+ āĻ¸āĻš āĻāĻ•āĻŸāĻŋ āĻ¸ā§‚āĻšāĻ•ā§‡āĻ° āĻĒāĻŋāĻ›āĻ¨ā§‡ āĻ¸āĻ°āĻžāĻ¸āĻ°āĻŋ āĻāĻ•āĻŸāĻŋ āĻŦāĻŋā§Ÿā§‹āĻ— āĻšāĻŋāĻšā§āĻ¨ āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻŋāĻ¤ āĻšā§ŸāĨ¤ āĻ¯āĻĻāĻŋ āĻ¸ā§‚āĻšāĻ•āĻŸāĻŋ āĻ§ā§āĻŦāĻ¨āĻžāĻ¤ā§āĻŦāĻ• āĻšā§Ÿ āĻ¤āĻžāĻšāĻ˛ā§‡ E+ āĻ…āĻĨāĻŦāĻž e+ āĻ¸āĻš āĻ¸ā§‚āĻšāĻ•ā§‡āĻ° āĻĒāĻŋāĻ›āĻ¨ā§‡ āĻļā§āĻ§ā§āĻŽāĻžāĻ¤ā§āĻ° āĻāĻ•āĻŸāĻŋ āĻ¯ā§‹āĻ— āĻšāĻŋāĻšā§āĻ¨ āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻŋāĻ¤ āĻšā§ŸāĨ¤

The thousands delimiter is displayed if the format code contains the delimiter enclosed by digit placeholders (0 or #).

āĻ¸āĻšāĻ¸ā§āĻ° āĻāĻŦāĻ‚ āĻĻāĻļāĻŽāĻŋāĻ• āĻŦāĻŋāĻ­āĻžāĻœāĻ• āĻšāĻŋāĻ¸ā§‡āĻŦā§‡ āĻĒāĻ°ā§āĻ¯āĻžāĻ¯āĻŧāĻ•āĻžāĻ˛ā§‡āĻ° āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻāĻ˛āĻžāĻ•āĻž āĻ­āĻŋāĻ¤ā§āĻ¤āĻŋāĻ• āĻ¸ā§‡āĻŸāĻŋāĻ‚ āĻāĻ° āĻ‰āĻĒāĻ° āĻ¨āĻŋāĻ°ā§āĻ­āĻ°āĻļā§€āĻ˛āĨ¤ āĻ¯āĻ–āĻ¨ āĻ†āĻĒāĻ¨āĻŋ āĻŽā§‚āĻ˛ āĻ¸ā§‹āĻ°ā§āĻ¸ āĻ•ā§‹āĻĄā§‡ āĻ¸āĻ°āĻžāĻ¸āĻ°āĻŋ āĻāĻ•āĻŸāĻŋ āĻ¸āĻ‚āĻ–ā§āĻ¯āĻž āĻ¸āĻ¨ā§āĻ¨āĻŋāĻŦā§‡āĻļ āĻ•āĻ°āĻžāĻ¨ā§‹āĻ° āĻ¸āĻŽā§Ÿ, āĻ¸āĻŦāĻ¸āĻŽāĻ¯āĻŧ āĻĻāĻļāĻŽāĻŋāĻ• āĻŦāĻŋāĻ­ā§‡āĻĻāĻ• āĻšāĻŋāĻ¸ā§‡āĻŦā§‡ āĻāĻ•āĻŸāĻŋ āĻĒāĻ°ā§āĻ¯āĻžāĻ¯āĻŧāĻ•āĻžāĻ˛ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§āĻ¨āĨ¤ āĻĻāĻļāĻŽāĻŋāĻ• āĻŦāĻŋāĻ­āĻžāĻœāĻ• āĻšāĻŋāĻ¸ā§‡āĻŦā§‡ āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻŋāĻ¤ āĻĒā§āĻ°āĻ•ā§ƒāĻ¤ āĻ…āĻ•ā§āĻˇāĻ°āĻŸāĻŋ āĻ†āĻĒāĻ¨āĻžāĻ° āĻ¸āĻŋāĻ¸ā§āĻŸā§‡āĻŽ āĻ¸ā§‡āĻŸāĻŋāĻ‚ āĻ āĻ¸āĻ‚āĻ–ā§āĻ¯āĻžāĻ° āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ā§‡āĻ° āĻ‰āĻĒāĻ° āĻ¨āĻŋāĻ°ā§āĻ­āĻ°āĻļā§€āĻ˛āĨ¤

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

āĻāĻ–āĻžāĻ¨ā§‡ āĻ¤āĻžāĻ˛āĻŋāĻ•āĻžāĻŦāĻĻā§āĻ§ āĻ¨ā§Ÿ āĻāĻŽāĻ¨ āĻ…āĻ•ā§āĻˇāĻ° āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻ¨ā§‡āĻ° āĻœāĻ¨ā§āĻ¯, āĻāĻŸāĻŋāĻ° āĻ…āĻŦāĻļā§āĻ¯āĻ‡ āĻāĻ•āĻŸāĻŋ āĻĒā§‚āĻ°ā§āĻŦāĻŦāĻ°ā§āĻ¤ā§€ āĻŦā§āĻ¯āĻžāĻ•āĻļā§āĻ˛ā§āĻ¯āĻžāĻļ (‌\\) āĻĨāĻžāĻ•āĻ¤ā§‡ āĻšāĻŦā§‡ āĻ…āĻĨāĻŦāĻž āĻāĻ•āĻŸāĻŋ āĻ‰āĻĻā§āĻ§ā§ƒāĻ¤āĻŋ āĻšāĻŋāĻšā§āĻ¨ (" ") āĻĻā§āĻŦāĻžāĻ°āĻž āĻ†āĻŦāĻĻā§āĻ§ āĻĨāĻžāĻ•āĻ¤ā§‡ āĻšāĻŦā§‡āĨ¤

\ : 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.

āĻ†āĻ•ā§āĻˇāĻ°āĻŋāĻ• āĻšāĻŋāĻ¸ā§‡āĻŦā§‡ āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻ¨ā§‡āĻ° āĻœāĻ¨ā§āĻ¯ āĻ…āĻ•ā§āĻˇāĻ°ā§‡āĻ° āĻĒā§‚āĻ°ā§āĻŦā§‡ āĻ…āĻŦāĻļā§āĻ¯āĻ‡ āĻāĻ•āĻŸāĻŋ āĻŦā§āĻ¯āĻžāĻ•āĻ¸ā§āĻ˛ā§āĻ¯āĻžāĻļ āĻŦāĻŋāĻļāĻŋāĻˇā§āĻŸ āĻ…āĻ•ā§āĻˇāĻ° āĻšāĻ˛ā§‹ āĻ¤āĻžāĻ°āĻŋāĻ– āĻāĻŦāĻ‚ āĻ¸āĻŽā§Ÿ āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ (a, c, d, h, m, n, p, q, s, t, w, y, /, :), āĻ¸āĻ‚āĻ–ā§āĻ¯āĻžāĻ¸ā§‚āĻšāĻ•-āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ āĻ…āĻ•ā§āĻˇāĻ° (#, 0, %, E, e, comma, period), āĻāĻŦāĻ‚ āĻˇā§āĻŸā§āĻ°āĻŋāĻ‚-āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ āĻ…āĻ•ā§āĻˇāĻ° (@, &, <, >, !)āĨ¤

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.

āĻ†āĻĒāĻ¨āĻŋ āĻ¯āĻĻāĻŋ āĻĒā§‚āĻ°ā§āĻŦāĻ¨āĻŋāĻ°ā§āĻ§āĻžāĻ°āĻŋāĻ¤ āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡āĻ¨ āĻ¤āĻžāĻšāĻ˛ā§‡, āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ā§‡āĻ° āĻ¨āĻžāĻŽāĻŸāĻŋ āĻ…āĻŦāĻļā§āĻ¯āĻ‡ āĻ‰āĻĻā§āĻ§ā§ƒāĻ¤āĻŋ-āĻšāĻŋāĻšā§āĻ¨ā§‡āĻ° āĻŽāĻ§ā§āĻ¯ā§‡ āĻ°āĻžāĻ–āĻ¤ā§‡ āĻšāĻŦā§‡āĨ¤

āĻĒā§‚āĻ°ā§āĻŦāĻ‰āĻ˛ā§āĻ˛ā§‡āĻ–āĻŋāĻ¤ āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸

āĻ¸āĻžāĻ§āĻžāĻ°āĻŖ āĻ¸āĻ‚āĻ–ā§āĻ¯āĻž: āĻ¸āĻ‚āĻ–ā§āĻ¯āĻž āĻĒā§āĻ°āĻŦā§‡āĻļā§‡āĻ° āĻ•ā§āĻ°āĻŽ āĻ…āĻ¨ā§āĻ¸āĻžāĻ°ā§‡ āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻŋāĻ¤ āĻšā§ŸāĨ¤

āĻŽā§‚āĻĻā§āĻ°āĻž: āĻ¸āĻ‚āĻ–ā§āĻ¯āĻžāĻ° āĻ¸āĻžāĻŽāĻ¨ā§‡ āĻāĻ•āĻŸāĻŋ āĻĄāĻ˛āĻžāĻ° āĻšāĻŋāĻšā§āĻ¨ ($) āĻ¸āĻ¨ā§āĻ¨āĻŋāĻŦā§‡āĻļ āĻ•āĻ°āĻžāĻ¨ā§‹ āĻšā§Ÿ āĻāĻŦāĻ‚ āĻ‹āĻ¨āĻžāĻ¤ā§āĻŦāĻ• āĻ¸āĻ‚āĻ–ā§āĻ¯āĻž āĻŦāĻ¨ā§āĻ§āĻ¨ā§€ āĻĻā§āĻŦāĻžāĻ°āĻž āĻ†āĻŦāĻĻā§āĻ§ āĻ•āĻ°āĻž āĻšā§ŸāĨ¤

āĻ¨āĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āĻŸ: āĻĻāĻļāĻŽāĻŋāĻ• āĻŦāĻŋāĻ­āĻžāĻœāĻ•ā§‡āĻ° āĻ¸āĻžāĻŽāĻ¨ā§‡ āĻ…āĻ¨ā§āĻ¤āĻ¤ āĻāĻ• āĻĄāĻŋāĻœāĻŋāĻŸ āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻŋāĻ¤ āĻšāĻŦā§‡āĨ¤

āĻ†āĻĻāĻ°ā§āĻļ: āĻāĻ•āĻŸāĻŋ āĻ¸āĻ‚āĻ–ā§āĻ¯āĻž āĻšāĻžāĻœāĻžāĻ° āĻŦāĻŋāĻ­āĻžāĻœāĻ• āĻ¸āĻš āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻŋāĻ¤ āĻšā§ŸāĨ¤

āĻļāĻ¤āĻ•āĻ°āĻž: āĻ¸āĻ‚āĻ–ā§āĻ¯āĻžāĻŸāĻŋāĻ•ā§‡ ā§§ā§Ļā§Ļ āĻĻā§āĻŦāĻžāĻ°āĻž āĻ—ā§āĻŖ āĻ•āĻ°āĻž āĻšā§Ÿ āĻāĻŦāĻ‚ āĻ¸āĻ‚āĻ–ā§āĻ¯āĻžāĻ° āĻļā§‡āĻˇā§‡ āĻāĻ•āĻŸāĻŋ āĻļāĻ¤āĻ•āĻ°āĻž āĻšāĻŋāĻšā§āĻ¨ āĻ¯ā§‹āĻ— āĻ•āĻ°āĻž āĻšā§ŸāĨ¤

āĻŦā§ˆāĻœā§āĻžāĻžāĻ¨āĻŋāĻ•: āĻŦā§ˆāĻœā§āĻžāĻžāĻ¨āĻŋāĻ• āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ā§‡ āĻ¸āĻ‚āĻ–ā§āĻ¯āĻž āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻŋāĻ¤ āĻšā§Ÿ (āĻ¯ā§‡āĻŽāĻ¨, 1000 āĻāĻ° āĻœāĻ¨ā§āĻ¯ 1.00E+03 )āĨ¤

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.

āĻ¤āĻžāĻ°āĻŋāĻ–, āĻ¸āĻŽāĻ¯āĻŧ āĻāĻŦāĻ‚ āĻŽā§āĻĻā§āĻ°āĻžāĻ° āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ā§‡āĻ° āĻœāĻ¨ā§āĻ¯ āĻ¸ā§āĻĨāĻžāĻ¨ā§€ā§Ÿ āĻ¸ā§‡āĻŸāĻŋāĻ‚ āĻ āĻāĻ•āĻ‡ āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ āĻĒā§āĻ°āĻ¯āĻŧā§‹āĻ— āĻ•āĻ°āĻž āĻšāĻ¯āĻŧāĨ¤ āĻāĻ‡ āĻŽā§‚āĻ˛ āĻŦāĻŋāĻ¨ā§āĻ¯āĻžāĻ¸ āĻ•ā§‹āĻĄāĻŸāĻŋ āĻ†āĻĒāĻ¨āĻžāĻ° āĻ¸ā§āĻĨāĻžāĻ¨ā§€āĻ¯āĻŧ āĻ¸ā§‡āĻŸāĻŋāĻ‚ āĻ…āĻ¨ā§āĻ¯āĻžāĻ¯āĻŧā§€ āĻŦā§āĻ¯āĻžāĻ–ā§āĻ¯āĻž āĻ•āĻ°āĻž āĻāĻŦāĻ‚ āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻ¨ āĻ•āĻ°āĻž āĻšāĻŦā§‡āĨ¤

Error codes:

5 Invalid procedure call

Example:


Sub ExampleFormat
    MsgBox Format(6328.2, "##,##0.00")
    REM āĻ¸āĻŦāĻ¸āĻŽā§Ÿ āĻĻāĻļāĻŽāĻŋāĻ• āĻŽāĻžāĻ¨ āĻšāĻŋāĻ¸ā§‡āĻŦā§‡ āĻāĻ•āĻŸāĻŋ āĻ•āĻžāĻ°ā§āĻ¯āĻ•āĻžāĻ˛ āĻŦā§āĻ¯āĻŦāĻšāĻžāĻ° āĻ•āĻ°ā§‡ āĻĨāĻžāĻ•ā§‡ āĻ¯āĻ–āĻ¨ āĻ†āĻĒāĻ¨āĻŋ āĻŽā§‚āĻ˛ āĻ¸ā§‹āĻ°ā§āĻ¸ āĻ•ā§‹āĻĄā§‡ āĻ¸āĻ‚āĻ–ā§āĻ¯āĻž āĻ¸āĻ¨ā§āĻ¨āĻŋāĻŦā§‡āĻļ āĻ•āĻ°āĻžāĻ¨āĨ¤
    REM āĻ‰āĻĻāĻžāĻšāĻ°āĻŖ āĻšāĻŋāĻ¸ā§‡āĻŦā§‡ ā§Ŧ,ā§Šā§¨ā§Ž.ā§¨ā§Ļ āĻ‡āĻ‚āĻ°ā§‡āĻœā§€ āĻ˛ā§‹āĻ•ā§āĻ¯āĻžāĻ˛, ā§Ŧ.ā§Šā§¨ā§Ž,ā§¨ā§Ļ āĻœāĻžāĻ°ā§āĻŽāĻžāĻ¨ āĻ˛ā§‹āĻ•ā§āĻ¯āĻžāĻ˛ āĻĒā§āĻ°āĻĻāĻ°ā§āĻļāĻ¨ āĻ•āĻ°ā§‡ āĻĨāĻžāĻ•ā§‡āĨ¤
End Sub

Please support us!