Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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