Last active
March 5, 2016 17:45
-
-
Save linggom/09652232ee1351760547 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import datetime | |
def convert_from_string(number): | |
nums = number.split(".") | |
real_num = 0 | |
count = len(nums) | |
for idx, num in enumerate(nums): | |
if idx < count - 1: | |
real_num += int(num) * (1000**(count - idx - 1)) | |
else: | |
real_num += int(num.split(",")[0]) #assuming we ignore the "," | |
return real_num | |
def convert_from_string_to_float(number): | |
number = str(number) | |
return float(".".join("".join(number.split(".")).split(","))) | |
def sanitize(number): | |
try: | |
return int(number) | |
except ValueError: | |
return convert_from_string(number) | |
if __name__ == '__main__': | |
t0 = datetime.datetime.now() | |
print sanitize('120.000') #slowest | |
print str(datetime.datetime.now() - t0) | |
t0 = datetime.datetime.now() | |
print convert_from_string_to_float(12000) #faster | |
print str(datetime.datetime.now() - t0) | |
print convert_from_string_to_float('1.200.000') | |
print sanitize(120000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment