Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FormatPercent Function (Visual Basic)

Returns an expression formatted as a percentage (that is, multiplied by 100) with a trailing % character.

Function FormatPercent(
   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
Expression

Required. Expression to be formatted.

NumDigitsAfterDecimal

Optional. Numeric value indicating how many places to the right of the decimal are displayed. Default value is –1, which indicates that the locale settings are used.

IncludeLeadingDigit

Optional. TriState constant that indicates whether or not a leading zero displays for fractional values. See "Settings" for values.

UseParensForNegativeNumbers

Optional. TriState constant that indicates whether or not 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.

The IncludeLeadingDigit, UseParensForNegativeNumbers, and GroupDigits arguments have the following settings.

Constant

Description

TriState.True

True

TriState.False

False

TriState.Default

The computer's regional settings

Exception type

Error number

Condition

InvalidCastException

13

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.

When one or more optional arguments are omitted, the values for omitted arguments are provided by the locale settings.

NoteNote:

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.

This example illustrates the use of the FormatPercent function.

Dim TestNumber As Single = 0.76
' Returns "76.00%". 
Dim TestString As String = FormatPercent(TestNumber)

Namespace: Microsoft.VisualBasic

Module: Strings

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.