VAL()
Jump to navigation
Jump to search
Description
- VAL() converts a string into a numeric value. If the string represents or starts with a numeric value, this value is returned; otherwise, zero is returned.
Syntax
- val(valid numeric string)
Hints
- Strings representing numbers can't be used in mathematical operations. They must first be converted to numeric values using VAL().
Example
' These strings will produce non-zero number in a VAL() function:
c$ = "12345"
d$ = "123 Main St."
e$ = "3 pages"
' These strings will produce zero in a VAL() function:
x$ = "Hello"
y$ = "Hello, you 3"
' Numbers which represent string values can't be used in mathematical operations.
' They must first be converted to numeric values.
' You cannot do this:
' print 5 * c$
' print 5 / "3"
' You can do this, though:
print 5 * val(c$)
print 5 / val("3")
print int(val("333 Main St."))
end
Useful Procedures
' Input with default value (if user just presses Enter, it returns default value)
' Usage example:
' x=inputDefault("Input x (default 10) ",10)
function inputDefault(prompt$, defaultVal)
input "";prompt$; aVal$
if trim$(aVal$)="" then
inputDefault = defaultVal
else
inputDefault = val(aVal$)
end if
end function