Export (0) Print
Expand All
12 out of 15 rated this helpful - Rate this topic

Val Function

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

Public Overloads Function Val(ByVal Expression As String) As Double

-or-

Public Overloads Function Val(ByVal Expression As Object) As Double

-or-

Public Overloads Function Val(ByVal Expression As Char) As Integer

Parameter

Expression
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.

Exceptions/Errors

Exception type Error number Condition
OverflowException 6 InputStr is too large.
InvalidCastException 13 Number is badly formed.
ArgumentException 438 Object type expression not convertible to String.

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 returns the value 1615198:

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

In the code below, Val returns the decimal value -1 for the hexadecimal value shown:

Val("&HFFFF")
Note   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.

Example

This 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
ValResult = Val("2457")    ' ValResult is set to 2457.
ValResult = Val(" 2 45 7")    ' ValResult is set to 2457.
ValResult = Val("24 and 57")    ' ValResult is set to 24.

Requirements

Namespace: Microsoft.VisualBasic

Module: Conversion

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

See Also

Str Function | Type Conversion Functions | OverflowException Class | InvalidCastException Class | ArgumentException Class

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.