VAL
From QB64 Wiki
The VAL Function returns the decimal numerical value of a STRING numerical value.
- value = VAL(string_value$)
- VAL stops reading the string if it encounters string characters other than numbers or a period (decimal point).
- If the first string character is not a number or period, VAL returns 0.
- VAL can return floating decimal values if it encounters ONE period.
- Warning: VAL will return erratic values with the "%" or "&" characters at a string's start.
- HEX$ string values with the "&H" prefix can be converted to a decimal value despite any A to F hex digits.
- OCT$ string values with the "&O" prefix can be converted to a decimal value.
- Presently VAL cannot convert QB64 binary &B prefixed strings from binary to decimal in QB64.
- For character values of ASCII data use ASC to get the value.
Example 1: Converting a string with some number characters
text$ = "1.23Hello" number! = VAL(text$) PRINT number!
1.23
Example 2:
a$ = "33" PRINT VAL("10") + VAL(a$) + 1
44
- Explanation: 10 + 33 + 1 = 44, the strings were converted to values.
- You have to convert the string to values in order to use them in a mathematical expression also since mixing strings with numbers isn't allowed. VAL will stop at a text letter so VAL("123G56) would return 123.
- If VAL wasn't used the program would break with error, as you can't add the value 1 to a string, if the 1 was a string ("1") then the program would return "10331", but now since we used VAL, the numbers were added as they should.
Example 3: Converting a hexadecimal value to decimal value using HEX$ with VAL.
decnumber% = 96 hexnumber$ = "&H" + HEX$(decnumber%) 'convert decimal value to hex and add hex prefix PRINT hexnumber$ decimal% = VAL(hexnumber$) PRINT decimal%
&H60 96
Explanation: HEX$ converts a decimal number to hexadecimal, but VAL will only recognize it as a valid value with the "&H" prefix. Especially since hexadecimal numbers can use "A" through "F" in them. Create a converter function from this code!
See also: