Hex()

You can represent hexadecimal numbers directly by preceding numbers in the proper range with &H. For example, &H10 represents decimal 16 in hexadecimal notation.

Example

This example uses the Hex function to return the hexadecimal value of a number.

Dim MyHex As String
MyHex = Hex(5)   ' Returns 5.
MyHex = Hex(10)   ' Returns A.
MyHex = Hex(459)   ' Returns 1CB.

 

Oct()

You can represent octal numbers directly by preceding numbers in the proper range with &O. For example, &O10 is the octal notation for decimal 8.

Example

This example uses the Oct function to return the octal value of a number.

Dim MyOct As String
MyOct = Oct(4)    ' Returns 4.
MyOct = Oct(8)   ' Returns 10.
MyOct = Oct(459)   ' Returns 713.

 

Val()

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.

 

Str()

When numbers are converted to strings, a leading space is always reserved for the sign of Number. If Number is positive, the returned string contains a leading space, and the plus sign is implied. A negative number will include the minus sign (-) and no leading space.

Use the Format function to convert numeric values you want formatted as dates, times, or currency or in other user-defined formats. Unlike the Str function, the Format function doesn't include a leading space for the sign of Number.

Note   The Str function recognizes only the period (.) as a valid decimal separator. If different decimal separators are used (for example, in international applications), use the CStr function to convert a number to a string.

Example

This example uses the Str function to return a String representation of a number. When a positive number is converted to a string, a leading space is always reserved for its sign.

Dim MyString As String
MyString = Str(459)   ' Returns " 459".
MyString = Str(-459.65)   ' Returns "-459.65".
MyString = Str(459.001)   ' Returns " 459.001".