Partager via


Conversion.Val Method (String)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the numbers contained in a string as a numeric value of appropriate type.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

Syntax

'Declaration
Public Shared Function Val ( _
    InputStr As String _
) As Double
public static double Val(
    string InputStr
)

Parameters

  • InputStr
    Type: System.String
    Required. Any valid String expression, Object variable, or Char value. If Expression is of type Object, its value must be convertible to String or an ArgumentException error occurs.

Return Value

Type: System.Double
Returns the numbers contained in a string as a numeric value of appropriate type.

Remarks

The Val function stops reading the string at the first character it cannot recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized. However, the function recognizes the radix prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are stripped from the argument.

The following call returns the value 1615198.

Val(" 1615 198th Street N.E.") 

The following call returns the decimal value -1.

Val("&HFFFF") 
NoteNote:

The Val function recognizes only the period (.) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl or CInt instead to convert a string to a number. To convert the string representation of a number in a particular culture to a numeric value, use the numeric type's Parse(String, IFormatProvider) method. For example, use Double.Parse when converting a string to a Double.

Examples

The following example uses the Val function to return the numbers contained in each string. Val stops converting at the first character that cannot be interpreted as a numeric digit, numeric modifier, numeric punctuation, or white space.

Dim valResult As Double
' The following line of code sets valResult to 2457.
valResult = Val("2457")
' The following line of code sets valResult to 2457.
valResult = Val(" 2 45 7")
' The following line of code sets valResult to 24.
valResult = Val("24 and 57")

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.