VAL: Difference between revisions
Jump to navigation
Jump to search
Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link
No edit summary |
No edit summary |
||
(4 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
{{PageSyntax}} | {{PageSyntax}} | ||
: {Parameter|value}} = '''VAL'''({{Parameter|string_value$}}) | : {{Parameter|value}} = '''VAL'''({{Parameter|string_value$}}) | ||
* VAL converts string numbers to numerical values including decimal point values and prefixed "[[&B]]" binary, "[[&H]]" hexadecimal, "[[&O]]" octal. | * VAL converts string numbers to numerical values including decimal point values and prefixed "[[&B]]" binary, "[[&H]]" hexadecimal, "[[&O]]" octal. | ||
* VAL conversion stops at non-numeric characters except for letter "D" or "E" exponential notation values. | * VAL conversion stops at non-numeric characters except for letter "D" or "E" exponential notation values. | ||
:String values with "D" and "E" letters between numbers may be converted also! EX: '''{{ | :String values with "D" and "E" letters between numbers may be converted also! EX: '''{{Text|VAL("9D4") <nowiki>=</nowiki> 90000|green}}''' | ||
* If the first string character is not a number VAL returns 0. VAL may return erratic values with "%" or "&" starting characters. | * If the first string character is not a number VAL returns 0. VAL may return erratic values with "%" or "&" starting characters. | ||
* Binary [[_BIN$]] string values with the "[[&B]]" prefix can be converted to a decimal value with digits from 0 to 1 only. | * Binary [[_BIN$]] string values with the "[[&B]]" prefix can be converted to a decimal value with digits from 0 to 1 only. | ||
* Hexadecimal [[HEX$]] string values with the "[[&H]]" prefix can be converted to a decimal value with digits 0 to 9 and letters A to F, like; dec = VAL("&H"+hexvar$). | * Hexadecimal [[HEX$]] string values with the "[[&H]]" prefix can be converted to a decimal value with digits 0 to 9 and letters A to F, like; dec = VAL("&H"+hexvar$). | ||
* Octal [[OCT$]] string values with the "[[&O]]" prefix can be converted to a decimal value with digits from 0 to 7 only. | * Octal [[OCT$]] string values with the "[[&O]]" prefix can be converted to a decimal value with digits from 0 to 7 only. | ||
* For character values of [[ASCII]] data use [[ASC]] to get the value. | * For character values of [[ASCII]] data use the [[ASC (function)]] to get the value. | ||
* In QB64 use an [[INTEGER]] return variable to hold integer values returned by VAL [[HEX$|Hex]] strings: '''{{ | * In QB64 use an [[INTEGER]] return variable to hold integer values returned by VAL [[HEX$|Hex]] strings: '''{{Text|value% <nowiki>= VAL("&HFFFF") =</nowiki> -1|green}}''' | ||
''Example 1:'' Differences in values returned with QBasic and QB64: | ''Example 1:'' Differences in values returned with QBasic and QB64: | ||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|PRINT}} {{Cl|VAL}}("{{Cl|&H}}") '203 in QB, 0 in QB64 | {{Cl|PRINT}} {{Cl|VAL}}("{{Cl|&H}}") '203 in QB, 0 in QB64 | ||
{{Cl|PRINT}} {{Cl|VAL}}("{{Cl|&H}}FFFF") ' -1 QB, 65535 in QB64 | {{Cl|PRINT}} {{Cl|VAL}}("{{Cl|&H}}FFFF") ' -1 QB, 65535 in QB64 | ||
{{Cl|PRINT}} {{Cl|VAL}}("{{Cl|&H}}FFFF&") '65535 in both | {{Cl|PRINT}} {{Cl|VAL}}("{{Cl|&H}}FFFF&") '65535 in both | ||
{{CodeEnd}} | {{CodeEnd}} | ||
:''Explanation:'' A quirk in QBasic returned VAL values of 203 for "&" and "&H" that was never fixed until PDS(7.1). | :''Explanation:'' A quirk in QBasic returned VAL values of 203 for "&" and "&H" that was never fixed until PDS(7.1). | ||
Line 27: | Line 27: | ||
''Example 2:'' Converting a string with some number characters | ''Example 2:'' Converting a string with some number characters | ||
{{CodeStart}} | {{CodeStart}} | ||
text$ = "1.23Hello" | text$ = "1.23Hello" | ||
number! = VAL(text$) | number! = VAL(text$) | ||
PRINT number! | PRINT number! | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
Line 38: | Line 38: | ||
''Example 3:'' Converting literal and variable [[STRING|string]] values to numerical values. | ''Example 3:'' Converting literal and variable [[STRING|string]] values to numerical values. | ||
{{CodeStart}} | {{CodeStart}} | ||
a$ = "33" | a$ = "33" | ||
PRINT VAL("10") + VAL(a$) + 1 | PRINT VAL("10") + VAL(a$) + 1 | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
Line 58: | Line 58: | ||
PRINT hexnumber$ | PRINT hexnumber$ | ||
decimal% = {{Cl|VAL}}(hexnumber$) | decimal% = {{Cl|VAL}}(hexnumber$) | ||
PRINT decimal% | PRINT decimal% | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{OutputStart}} | {{OutputStart}} | ||
Line 67: | Line 67: | ||
{{PageSeeAlso}} | |||
* [[ASC]], [[STR$]] | * [[ASC (function)]], [[STR$]] | ||
* [[_BIN$]], [[HEX$]], [[OCT$]] | * [[_BIN$]], [[HEX$]], [[OCT$]] | ||
{{PageNavigation}} | {{PageNavigation}} |
Latest revision as of 00:47, 26 February 2023
The VAL Function returns the decimal numerical equivalent value of a STRING numerical value.
Syntax
- value = VAL(string_value$)
- VAL converts string numbers to numerical values including decimal point values and prefixed "&B" binary, "&H" hexadecimal, "&O" octal.
- VAL conversion stops at non-numeric characters except for letter "D" or "E" exponential notation values.
- String values with "D" and "E" letters between numbers may be converted also! EX: VAL("9D4") = 90000
- If the first string character is not a number VAL returns 0. VAL may return erratic values with "%" or "&" starting characters.
- Binary _BIN$ string values with the "&B" prefix can be converted to a decimal value with digits from 0 to 1 only.
- Hexadecimal HEX$ string values with the "&H" prefix can be converted to a decimal value with digits 0 to 9 and letters A to F, like; dec = VAL("&H"+hexvar$).
- Octal OCT$ string values with the "&O" prefix can be converted to a decimal value with digits from 0 to 7 only.
- For character values of ASCII data use the ASC (function) to get the value.
- In QB64 use an INTEGER return variable to hold integer values returned by VAL Hex strings: value% = VAL("&HFFFF") = -1
Example 1: Differences in values returned with QBasic and QB64:
PRINT VAL("&H") '203 in QB, 0 in QB64 PRINT VAL("&HFFFF") ' -1 QB, 65535 in QB64 PRINT VAL("&HFFFF&") '65535 in both |
- Explanation: A quirk in QBasic returned VAL values of 203 for "&" and "&H" that was never fixed until PDS(7.1).
Example 2: Converting a string with some number characters
text$ = "1.23Hello" number! = VAL(text$) PRINT number! |
1.23 |
Example 3: Converting literal and variable string values to numerical values.
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 an 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 4: 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