VAL: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 8: | Line 8: | ||
{{PageParameters}} | {{PageParameters}} | ||
* {{Parameter|stringValue$}} is a [[STRING]] containing a sequence of digit characters which shall be converted into a numeric value. | * {{Parameter|stringValue$}} is a [[STRING]] containing a sequence of digit characters which shall be converted into a numeric value. | ||
** May also contain the letters '''E''', '''D''' and '''F''' for an exponent value in the [[scientific notation]] | ** May also contain the letters '''E''', '''D''' and '''F''' for an exponent value in the [[scientific notation]]. | ||
** The number as well as a given exponent value may be prepended with a ''plus(+)'' or ''minus(-)'' sign and any spaces in between the characters are ignored. | ** The number as well as a given exponent value may be prepended with a ''plus(+)'' or ''minus(-)'' sign and any spaces in between the characters are ignored. | ||
** The string may also start with [[&B]], [[&H]] or [[&O]] to denote a binary, hexadecimal or octal number string respectively. However, only leading spaces (before the '''&''' character) are ignored here. | ** The string may also start with [[&B]], [[&H]] or [[&O]] to denote a binary, hexadecimal or octal number string respectively. However, only leading spaces (before the '''&''' character) are ignored here. | ||
Line 33: | Line 33: | ||
{{PageExamples}} | {{PageExamples}} | ||
;Example 1: Differences in values returned with QBasic and QB64(PE). | |||
{{CodeStart}} | {{CodeStart}} | ||
{{Cl|PRINT}} {{Cl|VAL}}({{Text|<nowiki>"&H"</nowiki>|#FFB100}}) {{Text|<nowiki>'203 in QB, 0 in QB64</nowiki>|#919191}} | {{Cl|PRINT}} {{Cl|VAL}}({{Text|<nowiki>"&H"</nowiki>|#FFB100}}) {{Text|<nowiki>'203 in QB, 0 in QB64</nowiki>|#919191}} | ||
Line 39: | Line 39: | ||
{{Cl|PRINT}} {{Cl|VAL}}({{Text|<nowiki>"&HFFFF&"</nowiki>|#FFB100}}) {{Text|<nowiki>'65535 in both</nowiki>|#919191}} | {{Cl|PRINT}} {{Cl|VAL}}({{Text|<nowiki>"&HFFFF&"</nowiki>|#FFB100}}) {{Text|<nowiki>'65535 in both</nowiki>|#919191}} | ||
{{CodeEnd}} | {{CodeEnd}} | ||
{{PreStart}} | |||
'''Explanation''' | |||
A quirk in QBasic returned '''VAL''' values of 203 for '''&''' and [[&H]] that was | |||
never fixed until PDS(7.1). | |||
{{PreEnd}} | |||
---- | |||
;Example 2: Converting a string with some number characters in it. | |||
{{CodeStart}} | {{CodeStart}} | ||
text$ = {{Text|<nowiki>"1.23Hello"</nowiki>|#FFB100}} | text$ = {{Text|<nowiki>"1.23Hello"</nowiki>|#FFB100}} | ||
Line 52: | Line 57: | ||
{{OutputEnd}} | {{OutputEnd}} | ||
---- | |||
;Example 3: Converting a literal and a variable string to numerical values and add them. | |||
{{CodeStart}} | {{CodeStart}} | ||
a$ = {{Text|<nowiki>"33"</nowiki>|#FFB100}} | a$ = {{Text|<nowiki>"33"</nowiki>|#FFB100}} | ||
Line 61: | Line 67: | ||
44 | 44 | ||
{{OutputEnd}} | {{OutputEnd}} | ||
{{PreStart}} | |||
'''Explanation''' | |||
You have to convert the string to values in order to use them in a | |||
mathematical expression. If '''VAL''' wasn't used here, then the program | |||
would break with an error, as you can't add strings and numbers. | |||
{{PreEnd}} | |||
---- | |||
;Example 4: Converting a hexadecimal value to decimal value using [[HEX$]] with '''VAL'''. | |||
{{CodeStart}} | {{CodeStart}} | ||
decnumber% = {{Text|96|#F580B1}} | decnumber% = {{Text|96|#F580B1}} | ||
Line 80: | Line 88: | ||
96 | 96 | ||
{{OutputEnd}} | {{OutputEnd}} | ||
{{PreStart}} | |||
'''Explanation''' | |||
[[HEX$]] converts a decimal number to hexadecimal, but [[VAL]] will only | |||
recognize it as a valid value with the [[&H]] prefix. | |||
{{PreEnd}} | |||
Latest revision as of 19:03, 6 June 2025
The VAL function returns the decimal numerical equivalent value of a STRING numerical value.
Syntax
- numericValue = VAL(stringValue$)
Parameters
- stringValue$ is a STRING containing a sequence of digit characters which shall be converted into a numeric value.
- May also contain the letters E, D and F for an exponent value in the scientific notation.
- The number as well as a given exponent value may be prepended with a plus(+) or minus(-) sign and any spaces in between the characters are ignored.
- The string may also start with &B, &H or &O to denote a binary, hexadecimal or octal number string respectively. However, only leading spaces (before the & character) are ignored here.
Description
- The regular (decimal) conversion stops at non-numeric characters except for spaces and the letters E, D and F for specifying an exponent. But note, the string cannot be an exponent only, i.e. if the first non-space and non-sign string character is not a digit, then VAL returns zero(0). Same happens if it is a sign or spaces only.
- For binary, hexadecimal or octal strings conversion stops at digits or letters which are invalid in the respective number base system.
- Note that this function cannot be used to return the ASCII value of a string character, use the ASC (function) for that purpose.
Availability
-
all
-
all
-
yes
-
yes
-
yes
Examples
- Example 1
- Differences in values returned with QBasic and QB64(PE).
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 in it.
text$ = "1.23Hello" number! = VAL(text$) PRINT number! |
1.23 |
- Example 3
- Converting a literal and a variable string to numerical values and add them.
a$ = "33" PRINT VAL("10") + VAL(a$) + 1 |
44 |
Explanation You have to convert the string to values in order to use them in a mathematical expression. If VAL wasn't used here, then the program would break with an error, as you can't add strings and numbers. |
- 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. |
See also