Val
Result# = Val(InputString$)
 
Parameters:

    InputString$ = The string you wish to convert to a value
Returns:

    Result# = The returned value
 

      The Val function converts a string of numeric characters to a value. The function expects that the string to contain only valid numeric characters (0123456789), if not, you will get unexpected result. This function not only supports integer values and floating point values, but binary and hexadecimal values also.




FACTS:


      * Val can handle integer and float values. If you wish it to return a float use the Val#(), otherwise it'll return the value as an integer.

      * Val can handle Binary formatted strings (Bin$). To be evaluated as binary, val expects the first character to be the "%" symbol

      * Val can handle Hexadecimal formatted strings (Hex$). To be evaluated as hex, val expects the first character to be the "$" symbol




Mini Tutorial #1: Integer Conversion


      Converting a value to a string, then back again.

  
  
; Create an integer variable V and assign it the value 1000
  v=1000
  
; Convert the value contained with the variable V to a string form.
  s$=Str$(v)
  
; Convert the characters contained with the s$ variable
;  into a value, then add 10, and display the result.
  Print Val(s$)+10
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  



This example would output.

  
  1010
  







Mini Tutorial #2: Float conversion


      Converting a float value to a string, then back again.

  
  
; Create an float variable V# and assign it
; the float value 123.456
  v#=123.456
  
; Convert the value contained with the variable V# to a string form.
  s$=Str$(v#)
  
; Convert the characters contained with the s$ variable
;  back to a float value, then add 1000 to it.
  Print Val#(s$)+1000
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  
  







Mini Tutorial #3: Binary And Hex conversions


      Handling Binary and Hex strings.

  
  
;  store the number 255 in hex formatted string in s$
  s$="$ff"
  
; Convert the characters contained with the s$ variable
;  into a value, then adds 10 to it for display.
  Print Val(s$)+10
  
  
;  store the number 255 in binary formatted string in s$
  s$="%11111111"
  
; Convert the characters contained with the s$ variable
;  into a value, then adds 10 to it for display.
  Print Val(s$)+10
  
  
; Display the Screen and wait for the user to press a key
  Sync
  WaitKey
  
  



This example would output.

  
  265
  265
  




 
Related Info: Bin$ | Float | Hex$ | Integer | Str$ :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com