real = val(string$ [, Nth] [, string_options])
The VAL() function converts a string into a real number. By
default:
- If the input string is not purely numeric, the function raises an
exception.
- On success:
- The special variable
_integer
is set to one more
than the length of the successfully converted portion of the
string.
- On failure:
- The special variable
_integer
is set to
0
.
Index Behavior:
- When
Nth = 0
:
- The function expects the input string to be entirely
numeric.
- No exceptions are raised for non-numeric strings.
- If the string is invalid,
_integer
is set to
0
, and you can test its value after the function
call.
- When
Nth > 0
:
- The function attempts to extract the Nth numeric value from
the string.
- If no valid number is found,
_integer
is set to
0
after the function call.
Note:
The _integer
variable provides critical feedback on
the success of the function and the portion of the string
successfully parsed.
print val("123.45") + 1000
1123.45
x$ = '123abc'
print val(x$, 0)
0
print _integer
0
print val(x$, 1)
123
a$ = "There were 11 cats, each had $10,000.56"
cat_count = val(a$, 1) // extracts the 11
pocket_money = val(a$, 2) // extracts 10000.56
a$ = 'We bought 3 tickets. It was -5 degrees outside, We spent 150.34. (858) 829-9506.'
print a$
for i = 1 to 999
num = val(a$, i)
if _integer = 0 then exit for
print i, num
next i
The options help parse strings where the numeric is ambiguous such
as val("123,234.56", 1, 'sep=","') where the number we want is part of a
comma delimited string. This would result in the number 123.
However, val("123,234.56", 1) would result in the number 123234.56
Options are:
- sep -- the characters are separators ", "
- ignore -- Ignore the characters "@#"
- neg -- characters indicating a negative number "()"
- euro -- characters part of european currency ",."
print val("123,234.56", 1, 'sep=","')
123
print val("123,234.56", 1)
123234.56
a$ = "The invoice showed (5.612,34) credit"
opt$ = 'neg="()",euro=",."' // handle () for negative and Euros
print val(a$, 1, opt$)
Practical Use Cases for the VAL()
Function
-
Extracting Values From Product Descriptions
a$ = "Widget: $12.99, Weight: 3.45 lbs, Stock: 120"
price = val(a$, 1)
weight = val(a$, 2)
stock = val(a$, 3)
print "Price: "; price
print "Weight: "; weight
print "Stock: "; stock
Price: 12.99
Weight: 3.45
Stock: 120
-
Processing User Input
x$ = "Amount: $1234.56"
amount = val(x$, 1, 'sep="$"')
if _integer > 0 then
print "Valid amount: "; amount
else
print "Invalid input."
endif
Valid amount: 1234.56
-
Extracting Data From Financial Reports
report$ = "Revenue: €1.234,56; Expenses: €987,65"
opt$ = 'euro=",.",sep=";"'
revenue = val(report$, 1, opt$)
expenses = val(report$, 2, opt$)
print "Revenue: "; revenue
print "Expenses: "; expenses
Revenue: 1234.56
Expenses: 987.65
-
Parsing Sensor Data
log$ = "Temperature: 23.5°C, Humidity: 55%, Pressure: 1013 hPa"
temp = val(log$, 1)
humidity = val(log$, 2)
pressure = val(log$, 3)
print "Temperature: "; temp; "°C"
print "Humidity: "; humidity; "%"
print "Pressure: "; pressure; " hPa"
Temperature: 23.5°C
Humidity: 55%
Pressure: 1013 hPa
-
Extracting Phone Numbers From Text
text$ = "Contact: (555) 123-4567 or (555) 987-6543"
opt$ = 'sep=" "'
phone1 = val(text$, 1, opt$)
phone2 = val(text$, 2, opt$)
print "First Phone Number: "; phone1
print "Second Phone Number: "; phone2
First Phone Number: 5551234567
Second Phone Number: 5559876543