Last active
July 14, 2016 02:53
-
-
Save LotusChing/b1c3ce6b7ad3c702977cb17a40587450 to your computer and use it in GitHub Desktop.
Convert String to int , Such as '123' -> 123
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
def do(in_str): | |
out_num = 0 | |
multiplier = 1 | |
for x in range(0, len(in_str)): | |
out_num = out_num * 10 + ord(in_str[x]) - ord('0') | |
return out_num * multiplier | |
def str_to_int(in_str): | |
if '.' in in_str: | |
big_num, small_num = in_str.split('.') | |
beishu = len(small_num) | |
return do(big_num) + do(small_num) / 10 ** beishu | |
else: | |
return do(in_str) | |
print(str_to_int('23'), type(str_to_int('23'))) | |
print(str_to_int('23.3'), type(str_to_int('23.3'))) | |
print(str_to_int('23.33'), type(str_to_int('23.33'))) | |
print(str_to_int('23.333'), type(str_to_int('23.333'))) | |
print(str_to_int('6666666'), type(str_to_int('6666666'))) | |
''' | |
Debug | |
def to_int(in_str): | |
out_num = 0 | |
multiplier = 1 | |
for x in range(0, len(in_str)): | |
# 0 3 | |
out_num = out_num * 10 + ord(in_str[x]) - ord('0') | |
# 1 = 0 * 10 + 49 - 48 | |
# 12 = 1 * 10 + 50 - 48 | |
# 123 = 12 * 10 + 51 - 49 | |
print(out_num) | |
return out_num * multiplier | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment