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.


Syntax:


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

Return value:

String

Parameters:

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

numDigitsAfterDecimal: Optional. A numeric value specifying the number of digits that should be displayed after the decimal. If omitted, it defaults to the value -1, meaning that the default settings for user interface locale should be used.

includeLeadingDigit: Optional. A vbTriState enumeration value, specifying whether a leading zero should be displayed for fractional values.

useParensForNegativeNumbers: Optional. A vbTriState enumeration value specifying whether negative numbers should be encased in parenthesis.

groupDigits: Optional. A vbTriState enumeration value specifying the number should be grouped (into thousands, etc.), using the group delimiter that is specified on the system's regional settings.

Error codes:

13 Datu tipu neatbilstība

Example:


        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
    

Please support us!