Skip to content

Instantly share code, notes, and snippets.

@CanOfColliders
Last active December 17, 2015 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CanOfColliders/5675730 to your computer and use it in GitHub Desktop.
Save CanOfColliders/5675730 to your computer and use it in GitHub Desktop.
Python: Trys to cast a string into int or float if possible, won't throw an error if not, just returns the string untouched.
# trys to cast strings into float or int if possible, or returns it.
def castToType(n):
n = n.replace(",", ".")
try:
num = float(n)
if "." not in n:
return int(num)
return num
except ValueError:
return n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment