I have written a function that will do this for you. It's dependent upon two of my other functions that I will include here --- the function you're interested in is StringToFloat.
Let me know what you think!
print StringToFloat#("5.5")
wait key
end
Function StringToFloat#(s$)
remstart
************************************
* *
* StringToFloat function by *
* djlaptop - © 2002 *
* *
* Converts a string value to *
* a float. *
* *
* Since DBPro 1st release's *
* val command has a bug, it does *
* not properly convert strings to *
* floats. This is meant to *
* compensate until an official *
* patch. *
* *
* !!!!!!!!PLEASE NOTE!!!!!!! *
* *
* Once a patch is released that *
* fixes the string => float bug *
* in the VAL function, it will *
* BREAK this code --- this *
* StringToFloat function will *
* cease to work properly. Please *
* make a note of this when *
* using this in your code. *
* *
************************************
remend
rem save s$ --- some weird string bug seems to mess it up on instr()
ssave$ = s$
rem grab the base integer --- If the val() function worked properly,
rem this would return floats as well as integers. Once it's patched
rem and working properly, this next line will render the rest of the
rem function redundant.... You'll get double the decimal - so if you
rem put in "10.25" this function will end up returning 10.5.
baseint# = val(s$)
rem find the decimal
place# = instr(s$, ".", 0)
rem if no decimal found, just return the integer
if place# = 0 then exitfunction baseint#
rem grab the portion after the decimal as a string
decimalportion$ = right$(ssave$, place#-1)
rem convert the decimal portion to a whole integer
dec# = val(decimalportion$)
rem get the power of 10 we will need to divide by to convert back to a decimal
pwr = RaiseToPower#(10, len(decimalportion$))
rem Make the division and convert back to a decimal
dec# = dec# / pwr
rem add the decimal to the base integer
thefloat# = baseint# + dec#
endfunction thefloat#
function RaiseToPower#(base#, power#)
remstart
************************************
* *
* RaiseToPower function by *
* djlaptop - © 2002 *
* *
* Raises a number to a power. *
* Only works with integers. *
* *
* Examples: *
* *
* RaiseToPower (5, 2) *
* *
* returns 25. *
* *
* RaiseToPower (5, -2) *
* *
* returns 0.04. *
* *
************************************
remend
rem Make sure power is an integer. Base doesn't have to be.
power# = int(power#)
rem handle raising to 0 or 1 power. Easy stuff. =D
if power#=0 then exitfunction 1
if power#=1 then exitfunction base#
rem save original base for use later
originalbase# = base#
rem handle negative exponents
if power# < 0
for i=0 to abs(power#)
base# = base# / originalbase#
next i
exitfunction base#
endif
rem handle positive exponents
for i=2 to power#
base# = base# * originalbase#
next i
endfunction base#
function instr(string$, search$, CaseSensitive)
remstart
************************************
* INSTR function by djlaptop *
* *
* Searches for a string within *
* another string. Case insens- *
* itive. *
* *
* Usage Example: *
* *
* instr("abcdefg", "cd", false) *
* *
* returns "3" *
* *
* *
* Copyright © 2002, djlaptop *
************************************
remend
if len(search$) > len(string$) then exitfunction 0
if casesensitive = 0
search$=upper$(search$)
string$=upper$(string$)
endif
if search$ = string$ then exitfunction 1
for i=1 to len(string$)
match$=""
o=i
while len(match$) < len(search$)
match$=match$+mid$(string$, o)
o=o+1
endwhile
if match$ = search$ then exitfunction i
next i
endfunction 0