Last active
December 17, 2015 21:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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