FormatNumber Function [VBA]

Moduł
Spis treści
Indeks 🔎︎

# FormatNumber [VBA]

Returns a string with a number formatting applied to a numeric expression.

Ta funkcja lub stała jest włączona za pomocą instrukcji Opcja VBASupport 1 umieszczona przed kodem programu wykonywalnego w module.

## Składnia:

    FormatNumber( expression, numDigitsAfterDecimal as Integer, includeLeadingDigit as Integer,
     useParensForNegativeNumbers as Integer, groupDigits as Integer )

## Zwracana wartość:

String

## Parametry:

expression: Required. The numeric expression to be formatted.

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.

- vbTrue or -1: Display a leading zero.

- vbFalse or 0: Do not display leading zeros.

- vbUseDefaults or -2: Use the user interface locale settings. This is the default when omitted.

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

- vbTrue or -1: Use parenthesis for negative numbers.

- vbFalse or 0: Do not display parenthesis.

- vbUseDefaults or -2: Same as vbFalse. This is the default when omitted.

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.

- vbTrue or -1: Group digits.

- vbFalse or 0: Do not group digits.

- vbUseDefaults or -2: Same as vbFalse. This is the default when omitted.

## Kody błędów:

13 Niezgodność typów

## Przykład:

    Sub TestFormatNumeber

    testName = "Test 1: positive, 2 decimals"

    str2 = "12.20"

    str1 = FormatNumber("12.2", 2, vbFalse, vbFalse, vbFalse)

    msgbox( "FormatNumber returned: " + str1 + ", Expected: " + str2)

    testName = "Test 2: negative, 20 decimals, use leading zero"

    str2 = "-0.20000000000000000000"

    str1 = FormatNumber("-.2", 20, vbTrue, vbFalse, vbFalse)

    msgbox( "FormatNumber returned: " + str1 + ", Expected: " + str2)

    testName = "Test 3: negative, 20 decimals, no leading zero"

    str2 = "-.20000000000000000000"

    str1 = FormatNumber("-0.2", 20, vbFalse, vbFalse, vbFalse)

    msgbox( "FormatNumber returned: " + str1 + ", Expected: " + str2)

    testName = "Test 4: negative, no leading zero, use parens"

    str2 = "(.20)"

    str1 = FormatNumber("-0.2", -1, vbFalse, vbTrue, vbFalse)

    msgbox( "FormatNumber returned: " + str1 + ", Expected: " + str2)

    testName = "Test 5: negative, default leading zero, use parens"

    str2 = "(0.20)"

    str1 = FormatNumber("-0.2", -1, vbUseDefault, vbTrue, vbFalse)

    msgbox( "FormatNumber returned: " + str1 + ", Expected: " + str2)

    testName = "Test 6: group digits"

    str2 = "-12,345,678.00"

    str1 = FormatNumber("-12345678", -1, vbUseDefault, vbUseDefault, vbTrue)

    msgbox( "FormatNumber returned: " + str1 + ", Expected: " + str2)

    End Sub

Tematy pokrewne

Funkcje matematyczne w VBA