O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Val Function

Class

Microsoft.VisualBasic.Conversion

Syntax

Val(expression)
expression (required; String or Char)

Any string representation of a number

Return Value

A Double able to hold the number contained in expression

Description

Converts a string representation of a number into a Double

Rules at a Glance

  • The Val function starts reading the string with the leftmost character and stops at the first character that it does not recognize as being part of a valid number. For example, the statement:

    iNumber = Val("1A1")

    returns 1.

  • &O and &H (the octal and hexadecimal prefixes) are recognized by the Val function.

  • Currency symbols, such as $ and £, and delimiters, such as commas, are not recognized as numbers by the Val function.

  • The Val function only recognizes the period (.) as a decimal delimiter.

  • Prior to processing expression, Val removes spaces, tabs, and line-feed characters.

Programming Tips and Gotchas

If you are developing an international application, you should use the more modern, internationally aware CDbl function to convert strings to numbers, since CDbl can recognize all decimal separators.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required