python - issues converting string to number (float or integer) -
I am trying to convert the string into numbers. This can be a float, integer, or empty string. Return value: Return value: str3 = "" str2 = "0.0" str3 = "if not s: Return" "Let's return:" ValueError Return Ent: Return Float (s) Print ("str3 =" + str (num (str3)) print ("str2 =" + str (num (str2)) print ("str3 =" + str (num (str3)) "1.1" str4 = "10" print ("Str4 =" + str (num (str4)))
Therefore, output:
str1 = & lt; == OK str2 = 0.0 & Lt; == I want this integer as 0 str3 = 1.1 & lt; == OK str4 = 10 "<" < / P>
ex> is not a valid integer string if you want to round zero to round So if you do this after converting to float.
def num (s): if not s: returns "" Ia: return etc. valueError: F = float (s) if F% 1.0 & lt; 0.0005: Return Int (F) Other: Returns FIFI: Returns 0
Comments
Post a Comment