FormatPercent [VBA]

Returns a string with a number formatting applied to a numeric expression. A percent sign is appended to the returned string.

warning

This constant, function or object is enabled with the statement Option VBASupport 1 placed before the executable program code in a module.


语法:


      FormatPercent( expression, [numDigitsAfterDecimal As Integer], [includeLeadingDigit As Integer], _
          [useParensForNegativeNumbers As Integer], [groupDigits As Integer] ) As String
    

返回值:

String

参数:

expression: Required. A numeric expression to be formatted. If expression is a string, then the decimal and thousands separator need to be localized.

numDigitsAfterDecimal」: 可选。数字表达式,指定小数点后应显示的位数。若忽略,则默认为 -1,即使用用户界面区域的默认设置。

includeLeadingDigit」: 可选。vbTriState 枚举值,指定小数是否应显示开头的零。

useParensForNegativeNumbers」: 可选。vbTriState 枚举值,指定负数是否应被括号包围。

groupDigits」: 可选。vbTriState 枚举值,指定要分组的数字 (如每千位),使用系统区域设置指定的分组分隔符。

错误代码:

13 数据类型不匹配

示例:


        Sub TestFormatNumber
          Const UseComputerRegionalSettings = -1

          MsgBox FormatPercent(12.2, NumDigitsAfterDecimal:=2) ' 1220.00% if selected user interface is english

          MsgBox FormatPercent("-,2", 2, IncludeLeadingDigit:=vbTrue) ' -20,00% if french user interface

          MsgBox FormatPercent("-0.2", 2) ' -20.00% for en-US, -0,00 for fr-CA, de-AT or pt-BR

          MsgBox FormatPercent(-0.2, UseComputerRegionalSettings, UseParensForNegativeNumbers:=vbTrue) ' (20,00)% if pt-BR

          MsgBox FormatPercent("-0,2", UseComputerRegionalSettings, vbUseDefault, vbTrue) ' (20,00)% if german

          MsgBox FormatPercent("-12345678", -1, vbUseDefault, vbUseDefault, GroupDigits:=vbTrue) ' -1 234 567 800,00% for fr-BE

        End Sub
    

请支持我们!