Use of the Val () function of VB

Source: Internet
Author: User

In VB, you may want to convert the data to the numeric type, which can be an integer, a single precision, a double precision, or a floating point number, however, this function returns only the numbers contained in the string. If the first character is a letter or a non-numeric character, the function range is 0.

The help of VB6.0 is as follows:

Returns the number contained in the string, which is a proper type of value.

Syntax

Val (String)

NecessaryStringThe parameter can be any valid string expression.

Description

ValFunction that stops reading strings from the first character that cannot be recognized as a number. Symbols and characters that are considered to be part of the value, such as the US circle number and comma, cannot be recognized. However, functions can recognize carry-in symbols.&O(Octal) and&H(Hexadecimal ). Blank spaces, tabs, and line breaks are removed from parameters.

The following return value is 1615198:

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

In the following code,ValReturns the decimal value-1 for the hexadecimal value.

Val("&HFFFF")

Note: ValThe function will only (.) As an available decimal separator. When different decimal point separators are used, such as in international applicationsCDblTo convert a string to a number.

Val function example

This example usesValThe function returns the value contained in the string.

Dim MyValueMyValue = Val("2457")   ' Return 2457.MyValue = Val(" 2 45 7")   ' Return 2457.MyValue = Val("24 and 57")   ' Return 24.

If you want to use functions in VB in C #, add a reference to Microsoft. VisualBasic. dll and import the namespace.

Using Microsoft. VisualBasic; then use the static Val function in the Conversion class. You can also use a fully qualified name to introduce it, for example

Microsoft. VisualBasic. Conversion. Val ("123abc"); the returned result is 123.

Question: If you want to use functions in other languages, but you do not want to construct them yourself in the language, you can import the DLL of other languages and then reference the functions in it.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.