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.
FormatNumber( expression, numDigitsAfterDecimal as Integer, includeLeadingDigit as Integer, useParensForNegativeNumbers as Integer, groupDigits as Integer )
String
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.
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.
13 Data type mismatch
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