Val()

Syntax

Val(a$)

Remarks

Returns the numeric value of a$. A common use of Val() is to convert strings containing digit characters to numbers so they may be used in mathematical formulas. If a$ does not begin with a digit character, Val() returns 0 (zero). The a$ string can be as long as 255 characters.

Note

Whereas raw numbers in a WordBasic instruction are not affected by the current settings for decimal and thousands separators, the Val() function is. For example, if the thousands separator is a period, the instruction a = 1.001 sets a to 1.001 but the instruction a = Val("1.001") sets a to 1001.

Examples

In both of the following instructions, Val() returns 10:


num = Val("10")
num = Val("10 Apples")

In both of the following instructions, Val() returns 0 (zero):


num = Val("ten")
num = Val("Apartment 10")

The following example prompts the user to type a number, which the InputBox$() function returns as a string. Val() converts the string to a number and multiplies it by 12. Str$() converts the product to a string so that it can be displayed in a message box.


a$ = InputBox$("How many dozen apples?")
total = Val(a$) * 12
MsgBox a$ + " dozen equals" + Str$(total)

See Also

Str$()