[ create a new paste ] login | about

Link: http://codepad.org/9xIbFxJ1    [ raw code | output | fork ]

Python, pasted on Jul 24:
def num(s):

     if not s:
         return ""

     try:

        list_s = s.split(".")

        # If there is no fractional part or fractional part is 0 return int(s) else float(s)

        if ( len(list_s) == 1 ) or ( int(list_s[1]) == 0 ):
            return int(s) 
        else:
            return float(s)

     except: 
        return 0
        
str1 = ""
str2 = "0.0"
str3 = "1.1"
str4 = "10"

print("str1 = "+str(num(str1)))
print("str2 = "+str(num(str2)))
print("str3 = "+str(num(str3)))
print("str4 = "+str(num(str4)))


Output:
1
2
3
4
str1 = 
str2 = 0
str3 = 1.1
str4 = 10


Create a new paste based on this one


Comments: