Format 函数

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

语法:


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

参数:

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.

返回值:

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 #).

句点是否用作千位分隔符和小数分隔符取决于所在的地区设置。当直接在 Basic 源代码中输入数字时,始终使用句点作为小数分隔符。而实际的小数分隔符取决于系统中设置的数字格式。

- + $ ( ) 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、逗号、句点) 和字符串格式化字符 (@、&、<、>、!)。

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.

在使用预设格式时,格式名称的前后需要加上引号。

预定义格式

General Number」: 显示格式与输入格式相同的数字。

Currency」: 在数字前插入美元符号并在负数前后加上括号。

Fixed」: 小数分隔符前至少显示一位数字。

Standard」: 显示数字时带有千位分隔符。

Percent」: 将数字乘以 100,并附加百分号。

Scientific」:以科学计数法格式显示数字 (例如,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.

您可以在 - 语言设置 - 语言中设置用于控制 LibreOffice Basic 中的数字、日期及货币格式的locale. 在Basic的格式化代码中,总是使用点号 (.) 作为您的locale中定义的小数点符号的「占位符」,并且在实际使用中将会被替换为locale设置中对应的字符。

同样适用于日期、时间和货币格式的区域设置。Basic 格式的代码的解释和显示将遵循您的区域设置。

错误代码:

5 无效的过程调用

示例:


Sub ExampleFormat
    MsgBox Format(6328.2, "##,##0.00")
    ' 在 Basic 源代码中输入数字时,总是使用英文句号作为小数分隔符。
    ' 例如,在英语区域设置下显示为「6,328.20」,在德语区域设置下显示为「6.328,20」。
End Sub

请支持我们!