Bewerken

Share via


Conversion.Val Method

Definition

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

Overloads

Val(Object)

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

Val(Char)

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

Val(String)

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

Val(Object)

Source:
Conversion.vb
Source:
Conversion.vb
Source:
Conversion.vb

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

public:
 static double Val(System::Object ^ Expression);
public static double Val (object? Expression);
public static double Val (object Expression);
static member Val : obj -> double
Public Function Val (Expression As Object) As Double

Parameters

Expression
Object

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.

Returns

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

Exceptions

InputStr is too large.

Expression is an Object type expression that is not convertible to String.

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")

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 removed from the argument.

The following call returns the value 1234123.

Val(" 1234 123th Street N.E.")   

The following call returns the decimal value -1.

Val("&HFFFF")   

Note

The Val function recognizes only the period (.) as a valid decimal separator. When other 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.

Note

An InvalidCastException exception might be thrown in response to certain uncommon number formats. For example, the following code raises this exception.

' These examples cause run-time errors.

'Console.WriteLine(Val("1.34%"))

'Console.WriteLine(Val("1.34&"))

The conflict between the number formatted as a Double and the Integer and Long type characters would be caught by the compiler if the conflict were not contained in strings.

' These examples cause compiler errors.

'Dim m = 1.34%

'Dim n = 1.34&

See also

Applies to

Val(Char)

Source:
Conversion.vb
Source:
Conversion.vb
Source:
Conversion.vb

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

public:
 static int Val(char Expression);
public static int Val (char Expression);
static member Val : char -> int
Public Function Val (Expression As Char) As Integer

Parameters

Expression
Char

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.

Returns

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

Exceptions

InputStr is too large.

Expression is an Object type expression that is not convertible to String.

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")

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")   

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

An InvalidCastException exception might be thrown in response to certain uncommon number formats. For example, the following code raises this exception.

' These examples cause run-time errors.  
Console.WriteLine(Val("1.34%"))  
Console.WriteLine(Val("1.34&"))  

The conflict between the number formatted as a Double and the Integer and Long type characters would be caught by the compiler if it were not contained in a string.

' These examples cause compiler errors.  
Dim m = 1.34%  
Dim n = 1.34&

See also

Applies to

Val(String)

Source:
Conversion.vb
Source:
Conversion.vb
Source:
Conversion.vb

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

public:
 static double Val(System::String ^ InputStr);
public static double Val (string? InputStr);
public static double Val (string InputStr);
static member Val : string -> double
Public Function Val (InputStr As String) As Double

Parameters

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

Returns

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

Exceptions

InputStr is too large.

Expression is an Object type expression that is not convertible to String.

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")

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 removed from the argument.

The following call returns the value 1234123.

Val(" 1234 123th Street N.E.")   

The following call returns the decimal value -1.

Val("&HFFFF")   

Note

The Val function recognizes only the period (.) as a valid decimal separator. When other 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.

Note

An InvalidCastException exception might be raised in response to certain uncommon number formats. For example, the following code raises this exception.

' These examples cause run-time errors.

'Console.WriteLine(Val("1.34%"))

'Console.WriteLine(Val("1.34&"))

The conflict between the number formatted as a Double and the Integer and Long type characters would be caught by the compiler if the conflict were not contained in strings.

' These examples cause compiler errors.

'Dim m = 1.34%

'Dim n = 1.34&

See also

Applies to