Val

From Xojo Documentation

Revision as of 19:09, 19 November 2009 by WikiSysop (talk) (1 revision)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Description

Returns the numeric form of a string.


Syntax

result=Val(string) OR

result=stringVariable

Introduced 5.0

.Val

Part Type Description
result Double The numeric equivalent of the string passed, if a numeric equivalent exists. If the passed string does not contain a valid number, then the result is zero. REALbasic does not parse the string itself; rather it is passed to the operating system. This means that the behavior is platform-specific in the case of an undefined result.
string String Any valid string expression.
stringVariable String Any variable of type String.


Notes

Val recognizes prefixes &o (octal), &b (binary), and &h (hexadecimal). However, spaces are not allowed in front of the ampersand. That is, " &hFF" returns 0, but "&h FF" returns 255.

The CDbl function is the same as the Val function but is international-savvy. Val should generally be used to convert internal data, but not data entered by the user. Since it is not intended for input from the user, Val does not take separator characters into consideration. For example.

Dim d as Double
d=Val("10,000.95")

returns 10. Use CDbl for data that is from the user. CDbl handles imbedded separators in the input string.


Val returns zero if string contains no numbers, except in the special case where the string begins with the string "NAN". In this case, it returns "NAN(021)".


Examples

These examples use the Val function to return the numbers contained in a string.

Dim n As Double
n = Val("12345") //returns 12345
n = Val("54.05car45") //returns 54.05
n = Val("123.45") //returns 123.45
n = Val("123 45") //returns 123
n = Val("123,456") //returns 123
n = Val("auto") //returns 0


n = Val("&hFFF") //returns 4095
n = Val("&b1111") //returns 15


Dim s as String
s="12345"
n=s.Val //returns 12345


See Also

CDbl, CStr, Str functions; &b, &h, &o literals.