Contents

# FormatNumber [VBA]

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

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

### Syntax:

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

### Return type:

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.

- vbTrue or -1: Display a leading zero.

- vbFalse or 0: Do not display leading zeros.

- vbUseDefault 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.

- vbUseDefault 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.

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

### Error codes:

13 Data type mismatch

### Example:

            Sub TestFormatNumber
            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

Related Topics

VBA Mathematical functions

Please support us!