Parse a string and verify it is a float.
In the above diagram:
DIGIT | → | the characters "0" thru "9" |
Ee | → | the character "E" or "e" |
EOS | → | end of string |
PERIOD | → | the period character (".") |
SIGN | → | the character "+" or "-" |
Note: The starting and ending spaces have been removed from the string.
# ========================================================== # Test String to Float Conversion # ---------------------------------------------------------- # This is not the most efficient code. I designed it as # a demonstration. It could be more efficient. # ---------------------------------------------------------- # Note: In this code the exponent must be an integer. # The pow() function can take a float. # ========================================================== # ---------------------------------------------------------- # global variables # ---------------------------------------------------------- e = 0 # string's exponent value ep = True # exponent is positive f = 0.0 # string's fraction value i = 0 # string character index l = 0 # string length n = 0.0 # string's number value np = True # number is positive v = 0.0 # string's float value # ---------------------------------------------------------- # convert a string to float # float conversion results is in global v # ---------------------------------------------------------- def StringToFloat(str): global e,ep,f,i,l,n,np,v # --- initialize e = 0 ep = True f = 0.0 i = 0 l = len(str) n = 0.0 np = True v = 0.0 # --- process string number print('GetNumber({}) i={}'.format(str,i)) if l < 1: return False if not GetNumber(str): return False # --- process string fraction print('GetFraction({}) i={}'.format(str,i)) if i < l: if not GetFraction(str): return False # --- process string exponent print('GetExponent({}) i={}'.format(str,i)) if i < l: if not GetExponent(str): return False # --- calculate float value nf = n + f # number + fraction if not np: # negative value? nf = -nf if not ep: # negative exponent? e = -e print('n={},f={},nf={},e={}'.format(n,f,nf,e)) if e == 0: v = nf else: v = pow(nf,e) return True # success # ---------------------------------------------------------- # get number part of float # ---------------------------------------------------------- def GetNumber(str): global i,l,n,np sign = False # found a [+-] character while i < l: c = str[i] # get a string character print("c={},n={},i={}".format(c,n,i)) i = i + 1 # increment string index if c == "+": # character "+" if sign: # found more that one sign? return False # error sign = True # found + sign elif c == "-": # character "-" if sign: # found more that one sign? return False # error sign = True # found - sign np = False # number is negative elif c == ".": # character "." return True elif c == "0": # character "0" n = n*10.0 elif c == "1": # character "1" n = (n*10.0) + 1.0 elif c == "2": # character "2" n = (n*10.0) + 2.0 elif c == "3": # character "3" n = (n*10.0) + 3.0 elif c == "4": # character "4" n = (n*10.0) + 4.0 elif c == "5": # character "5" n = (n*10.0) + 5.0 elif c == "6": # character "6" n = (n*10.0) + 6.0 elif c == "7": # character "7" n = (n*10.0) + 7.0 elif c == "8": # character "8" n = (n*10.0) + 8.0 elif c == "9": # character "9" n = (n*10.0) + 9.0 else: # illegal character return False return True # end of string # ---------------------------------------------------------- # get fraction part of float # ---------------------------------------------------------- def GetFraction(str): global i,f x = 0.1 # decimal place factor while i < l: c = str[i] # get string character print("c={},f={},i={},x={}".format(c,f,i,x)) i = i + 1 # increment string index if c == "E" or c == "e": # found exponent? return True # yes if c == "1": # character "1" f = f + (1.0*x) elif c == "2": # character "2" f = f + (2.0*x) elif c == "3": # character "3" f = f + (3.0*x) elif c == "4": # character "4" f = f + (4.0*x) elif c == "5": # character "5" f = f + (5.0*x) elif c == "6": # character "6" f = f + (6.0*x) elif c == "7": # character "7" f = f + (7.0*x) elif c == "8": # character "8" f = f + (8.0*x) elif c == "9": # character "9" f = f + (9.0*x) else: # illegal character return False x = x * 0.1 # decimal place factor return True # end of string # ---------------------------------------------------------- # get exponent part of float # ---------------------------------------------------------- def GetExponent(str): global i,e,ep e = 0 # initialize exponent to 0 sign = False # found a [+-] character while i < l: c = str[i] # get string character print('c={},e={},i={}'.format(c,e,i)) i = i + 1 # increment string index if c == "+": # character "+" if sign: # found more that one sign? return False # error sign = True # found + sign elif c == "-": # character "-" if sign: # found more that one sign? return False # error sign = True # found - sign ep = False # exponent is negative elif c == "0": # character "0" e = e*10 elif c == "1": # character "1" e = (e*10) + 1 elif c == "2": # character "2" e = (e*10) + 2 elif c == "3": # character "3" e = (e*10) + 3 elif c == "4": # character "4" e = (e*10) + 4 elif c == "5": # character "5" e = (e*10) + 5 elif c == "6": # character "6" e = (e*10) + 6 elif c == "7": # character "7" e = (e*10) + 7 elif c == "8": # character "8" e = (e*10) + 8 elif c == "9": # character "9" e = (e*10) + 9 else: # illegal character return False return True # end of string # --------------------------------------------------------- # main # --------------------------------------------------------- if __name__ == '__main__': # --- python version function ------------------------- import sys def RunningPython3(): ##print(sys.version_info) if sys.version_info[0] == 3: return True return False # --- get use input function -------------------------- def GetUserInput(prompt,py3): if py3: return input(prompt) else: return raw_input(prompt) # --- pause the program function ---------------------- def Pause(py3): GetUserInput('\nPress enter to continue ',py3) # --- ask the user for input -------------------------- py3 = RunningPython3() while True: s = GetUserInput('\nEnter float number: ',py3) s = s.strip() # remove leading/trailing spaces if s == '': # empty string? break if StringToFloat(s): print('Got Value {}'.format(v)) else: print('Oops! an error') Pause(py3)