VAL()

From Liberty BASIC Family
Revision as of 14:25, 14 September 2020 by StPendl (talk | contribs) (Text replacement - "Building Blocks Categories" to "Building Blocks")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Supported in Just BASIC Supported in Liberty BASIC Not supported in Liberty BASIC 5 Not supported in Run BASIC

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

  1. 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