Sheerpower Logo F.6  Extracting Numbers From Text -- Strings to Reals
real = val(string$ [,integer_index][,string_options])
The VAL() function is used to convert a string into a real number. By default, it will raise an exception if the input string does not contain a purely numeric representation.

When the index is 0, it also expects the input string to consist entirely of a numeric representation. However, it never raises an exception. Instead the special variable "_integer" is set to 0 and can be tested after the call.

If you use a non-zero index, VAL() will attempt to extract the Nth number from the string. Again, after the function call, the special variable "_integer" will be set to zero if no valid number is found in the string.

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$)
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.
Wide screen