Python程序将十进制浮点数转换为八进制数
给定一个浮点十进制值并输入小数位数,我们的任务是将其转换为八进制形式。
首先,我们从浮点值中取整数部分并将其转换为八进制,然后将其取小数部分并将其转换为八进制形式,最后将两者合并。
因此,第一步是取整数部分,并将数字除以8,然后记下余数,直到且除非股息小于8,然后将所有余数一起复制。
第二步是小数部分,继续将小数部分乘以8,直到并且除非我们得到0作为小数部分,然后再乘以第一次记下整数部分,然后再次将新值的小数部分乘以8,然后继续直到达到完美数字为止。
范例程式码
def float_convert_octal(my_number, places = 3):
my_whole, my_dec = str(my_number).split(".")
my_whole = int(my_whole)
my_dec = int (my_dec)
res = oct(my_whole).lstrip("0o") + "."
for x in range(places):
my_whole, my_dec = str((decimal_converter(my_dec)) * 8).split(".")
my_dec = int(my_dec)
res += my_whole
return res
def decimal_converter(num):
while num > 1:
num /= 10
return num
n = input("Enter the floating point value : \n")
p = int(input("Enter the number of decimal places of the result : \n"))
print(float_convert_octal(n, places = p))输出结果
Enter the floating point value : 6.89 Enter the number of decimal places of the result : 12 6.707534121727
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便