|
|
Title | Use the FormatPercent function in Visual Basic 6 |
Description | This example shows how to use the FormatPercent function in Visual Basic 6. |
Keywords | FormatPercent, format percent |
Categories | Strings |
|
|
The FormatPercent function returns a formatted string representation for a number representing a percentage. It is very similar to FormatNumber except it multiplies the number by 100 and adds a percent character to the result. The syntax is:
FormatPercent(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 (in the United States):
Expression | Result |
FormatPercent(1.23456, 2) | 123.46% |
FormatPercent(0.001234, 2, TriState.False) | 0.12% |
FormatPercent(0.001234, 2, TriState.True) | .12% |
FormatPercent(-12345.12, , TriState.False) | -1,234,512.00% |
FormatPercent(-12345.12, , TriState.True) | (1,234,512.00%) |
FormatPercent(-12345.12, , TriState.True, TriState.False) | (1234512.00%) |
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 & "FormatPercent(" & Format$(x) & ", 2) = " & _
FormatPercent(x, 2) & vbCrLf
x = 0.001234
txt = txt & "FormatPercent(" & Format$(x) & ", 2, " & _
"vbFalse) = " & FormatPercent(x, 2, vbFalse) & _
vbCrLf
txt = txt & "FormatPercent(" & Format$(x) & ", 2, " & _
"vbTrue) = " & FormatPercent(x, 2, vbTrue) & vbCrLf
x = -12345.12345
txt = txt & "FormatPercent(" & Format$(x) & ", , " & _
"vbFalse) = " & FormatPercent(x, 2, , vbFalse) & _
vbCrLf
txt = txt & "FormatPercent(" & Format$(x) & ", , " & _
"vbTrue) = " & FormatPercent(x, 2, , vbTrue) & _
vbCrLf
txt = txt & "FormatPercent(" & Format$(x) & ", , " & _
"vbTrue, vbFalse) = " & FormatPercent(x, 2, , _
vbTrue, vbFalse) & vbCrLf
txtResults.Text = txt
End Sub
|
|
|
|
|
|