VAL: Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
The '''VAL''' Function returns the decimal numerical equivalent value of a [[STRING]] numerical value.
The '''VAL''' function returns the decimal numerical equivalent value of a [[STRING]] numerical value.




{{PageSyntax}}
{{PageSyntax}}
: {{Parameter|value}} = '''VAL'''({{Parameter|string_value$}})
: {{Parameter|numericValue}} = [[VAL]]({{Parameter|stringValue$}})




* VAL converts string numbers to numerical values including decimal point values and prefixed "[[&B]]" binary, "[[&H]]" hexadecimal, "[[&O]]" octal.
{{PageParameters}}
* VAL conversion stops at non-numeric characters except for letter "D" or "E" exponential notation values.
* {{Parameter|stringValue$}} is a [[STRING]] containing a sequence of digit characters which shall be converted into a numeric value.
:String values with "D" and "E" letters between numbers may be converted also! EX: '''{{text|VAL("9D4") <nowiki>=</nowiki> 90000|green}}'''
** May also contain the letters '''E''', '''D''' and '''F''' for an exponent value in the [[scientific notation]].
* If the first string character is not a number VAL returns 0. VAL may return erratic values with "%" or "&" starting characters.
** 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.
* Binary [[_BIN$]] string values with the "[[&B]]" prefix can be converted to a decimal value with digits from 0 to 1 only.
** 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.
* 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 [[ASC]] to get the value.
* 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:
{{PageDescription}}
* 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.
 
 
{{PageAvailability}}
<!-- QB64 = a version or none, QBPE = a version or all, Platforms = yes or no -->
<gallery widths="48px" heights="48px" mode="nolines">
File:Qb64.png|'''all'''
File:Qbpe.png|'''all'''
File:Apix.png
File:Win.png|'''yes'''
File:Lnx.png|'''yes'''
File:Osx.png|'''yes'''
</gallery>
<!-- additional availability notes go below here -->
 
 
{{PageExamples}}
;Example 1: Differences in values returned with QBasic and QB64(PE).
{{CodeStart}}
{{CodeStart}}
{{Cl|PRINT}} {{Cl|VAL}}("{{Cl|&H}}") '203 in QB, 0 in QB64
{{Cl|PRINT}} {{Cl|VAL}}({{Text|<nowiki>"&amp;H"</nowiki>|#FFB100}}) {{Text|<nowiki>'203 in QB, 0 in QB64</nowiki>|#919191}}
{{Cl|PRINT}} {{Cl|VAL}}("{{Cl|&H}}FFFF") ' -1 QB, 65535 in QB64
{{Cl|PRINT}} {{Cl|VAL}}({{Text|<nowiki>"&amp;HFFFF"</nowiki>|#FFB100}}) {{Text|<nowiki>' -1 QB, 65535 in QB64</nowiki>|#919191}}
{{Cl|PRINT}} {{Cl|VAL}}("{{Cl|&H}}FFFF&") '65535 in both
{{Cl|PRINT}} {{Cl|VAL}}({{Text|<nowiki>"&amp;HFFFF&amp;"</nowiki>|#FFB100}}) {{Text|<nowiki>'65535 in both</nowiki>|#919191}}
{{CodeEnd}}
{{CodeEnd}}
:''Explanation:'' A quirk in QBasic returned VAL values of 203 for "&" and "&H" that was never fixed until PDS(7.1).
{{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
;Example 2: Converting a string with some number characters in it.
{{CodeStart}}
{{CodeStart}}
text$ = "1.23Hello"
text$ = {{Text|<nowiki>"1.23Hello"</nowiki>|#FFB100}}
number! = VAL(text$)
number! = {{Cl|VAL}}(text$)
PRINT number!
{{Cl|PRINT}} number!
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 36: Line 57:
{{OutputEnd}}
{{OutputEnd}}


----


''Example 3:'' Converting literal and variable [[STRING|string]] values to numerical values.
;Example 3: Converting a literal and a variable string to numerical values and add them.
{{CodeStart}}
{{CodeStart}}
a$ = "33"
a$ = {{Text|<nowiki>"33"</nowiki>|#FFB100}}
PRINT VAL("10") + VAL(a$) + 1
{{Cl|PRINT}} {{Cl|VAL}}({{Text|<nowiki>"10"</nowiki>|#FFB100}}) + {{Cl|VAL}}(a$) + {{Text|1|#F580B1}}
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
44
44
{{OutputEnd}}
{{OutputEnd}}
:''Explanation:'' 10 + 33 + 1 = 44, the strings were converted to values.
{{PreStart}}
 
'''Explanation'''
: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.
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
: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.
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.
;Example 4: Converting a hexadecimal value to decimal value using [[HEX$]] with '''VAL'''.
{{CodeStart}}
{{CodeStart}}
decnumber% = 96
decnumber% = {{Text|96|#F580B1}}
hexnumber$ = "&H" + {{Cl|HEX$}}(decnumber%) 'convert decimal value to hex and add hex prefix
hexnumber$ = {{Text|<nowiki>"&amp;H"</nowiki>|#FFB100}} + {{Cl|HEX$}}(decnumber%) {{Text|<nowiki>'convert decimal value to hex and add hex prefix</nowiki>|#919191}}
PRINT hexnumber$
{{Cl|PRINT}} hexnumber$
decimal% = {{Cl|VAL}}(hexnumber$)
decimal% = {{Cl|VAL}}(hexnumber$)
PRINT decimal%
{{Cl|PRINT}} decimal%
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}
{{OutputStart}}
Line 64: Line 88:
  96
  96
{{OutputEnd}}
{{OutputEnd}}
: ''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!
{{PreStart}}
'''Explanation'''
[[HEX$]] converts a decimal number to hexadecimal, but [[VAL]] will only
recognize it as a valid value with the [[&H]] prefix.
{{PreEnd}}




{{PageSeeAlso}}
{{PageSeeAlso}}
* [[ASC]], [[STR$]]
* [[ASC (function)]], [[STR$]]
* [[_BIN$]], [[HEX$]], [[OCT$]]
* [[_BIN$]], [[HEX$]], [[OCT$]]




{{PageNavigation}}
{{PageNavigation}}

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


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



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link