FormatNumber Function (Visual Basic)
Returns an expression formatted as a number.
Function FormatNumber(
ByVal Expression As Object,
Optional ByVal NumDigitsAfterDecimal As Integer = -1,
Optional ByVal IncludeLeadingDigit As TriState = TriState.UseDefault,
Optional ByVal UseParensForNegativeNumbers As TriState = TriState.UseDefault,
Optional ByVal GroupDigits As TriState = TriState.UseDefault
) As String
Parameters
- Expression
Required. Expression to be formatted.
- NumDigitsAfterDecimal
Optional. Numeric value indicating how many places are displayed to the right of the decimal. The default value is –1, which indicates that the computer's regional settings are used.
- IncludeLeadingDigit
Optional. TriState constant that indicates whether a leading 0 is displayed for fractional values. See "Settings" for values.
- UseParensForNegativeNumbers
Optional. TriState constant that indicates whether to place negative values within parentheses. See "Settings" for values.
- GroupDigits
Optional. TriState constant that indicates whether or not numbers are grouped using the group delimiter specified in the locale settings. See "Settings" for values.
Settings
The IncludeLeadingDigit, UseParensForNegativeNumbers, and GroupDigits arguments have the following settings.
Constant | Description |
---|---|
TriState.True |
True |
TriState.False |
False |
TriState.UseDefault |
The computer's regional settings |
Exceptions
Exception type | Error number | Condition |
---|---|---|
Type is not numeric. |
See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.
Remarks
When one or more optional arguments are omitted the values for omitted arguments are provided by the locale settings.
Note
All settings information comes from the locale of the application. By default, that will be the locale set in the control panel. However, it may be changed programmatically by using the .NET Framework.
Example
This example demonstrates the FormatNumber function.
Dim TestNumber As Integer = 45600
' Returns "45,600.00".
Dim TestString As String = FormatNumber(TestNumber, 2, , , TriState.True)
Requirements
Namespace: Microsoft.VisualBasic
Module: Strings
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
See Also
Reference
String Manipulation Summary
FormatCurrency Function (Visual Basic)
FormatDateTime Function (Visual Basic)
FormatPercent Function (Visual Basic)
TriState Enumeration
InvalidCastException