Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse the FormatNumber function in Visual Basic 6
DescriptionThis example shows how to use the FormatNumber function in Visual Basic 6.
KeywordsFormatNumber, format number
CategoriesStrings
 
The FormatNumber function returns a formatted string representation for a number. The syntax is:

    FormatNumber(expression _
        [, digits_after_decimal] _
        [, include_leading_zero] _
        [, use_parens_if_negative] _
        [, groups_digits]

Where:

expression
The numeric expression to format

digits_after_decimal
The number of digits to display after the decimal point

include_leading_zero
If the number is less than 1 and greater than -1, determines whether the number should have a leading 0 before the decimal point.

use_parens_if_negative
Determines whether negative numbers are surrounded with parentheses instead of using a minus sign.

groups_digits
Determines whether digits to the left of the decimal point are grouped with thousands separators (commas in the United States).

Examples:

ExpressionResult
FormatNumber(1.23456, 2)1.23
FormatNumber(0.123456, 2, vbFalse).12
FormatNumber(0.123456, 2, vbTrue)0.12
FormatNumber(-12345.12, , vbFalse)-12,345.12
FormatNumber(-12345.12, , vbTrue)(12,345.12)
FormatNumber(-12345.12, , vbTrue, vbFalse)(12345.12)

This example uses the following code to display these examples in a TextBox.

 
Private Sub Form_Load()
    Dim txt As String
    Dim x As Single

    txt = ""
    x = 1.23456
    txt = txt & "FormatNumber(" & Format$(x) & ", 2) = " & _
        FormatNumber(x, 2) & vbCrLf

    x = 0.123456
    txt = txt & "FormatNumber(" & Format$(x) & ", 2, " & _
        "vbFalse) = " & FormatNumber(x, 2, vbFalse) & vbCrLf
    txt = txt & "FormatNumber(" & Format$(x) & ", 2, " & _
        "vbTrue) = " & FormatNumber(x, 2, vbTrue) & vbCrLf

    x = -12345.12345
    txt = txt & "FormatNumber(" & Format$(x) & ", , " & _
        "vbFalse) = " & FormatNumber(x, 2, , vbFalse) & _
        vbCrLf
    txt = txt & "FormatNumber(" & Format$(x) & ", , vbTrue) " & _
        "= " & FormatNumber(x, 2, , vbTrue) & vbCrLf
    txt = txt & "FormatNumber(" & Format$(x) & ", , vbTrue, " & _
        "vbFalse) = " & FormatNumber(x, 2, , vbTrue, _
        vbFalse) & vbCrLf

    txtResults.Text = txt
End Sub
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated