小数部分函数单独运行结果正确,但是一加整数部分函数,小数部分函数运行结果就不正确了。
number = eval(input('请输入一个十进制数:'))
#转二进制:
#整数部分:
b_number = number
xb_number = b_number - int(b_number)
xb_new_list = b_new_list = []
xB_number = B_number = ''
while b_number != 0:
by_number = b_number % 2
b_new_list.append(by_number)
b_number //= 2
b_new_list.reverse()
for Bin in b_new_list:
B_number =B_number + str(Bin)[0:1]
#小数部分:
while xb_number != 0:
xb_number = xb_number * 2
xb_new_list.append(int(xb_number))
xb_number = xb_number - int(xb_number)
for x_Bin in xb_new_list :
xB_number += str(x_Bin)[0:1]
print('对应的二进制数为:',B_number + '.' + xB_number)