简体   繁体   English

在python中将十进制分数转换为二进制

[英]Converting Decimal Fractions to Binary in python

I am trying to convert decimal fractions to binary in python.我正在尝试在 python 中将十进制分数转换为二进制。 Can anyone help谁能帮忙

def deci_to_bin(n):
    c=n%2
    print c
    n=n/2
    if n == 1:
        print n
        print n "conversaion of decimal to binary is given above see button to top"

    else:
        return deci_to_bin(n)

n=int(raw_input("enter decimal digit"))
deci_to_bin(n)

So far I got this but I keep on getting an error到目前为止,我得到了这个,但我不断收到错误

您需要单独print参数 -> print n, "your text"

You can always use the Binary fractions package.您始终可以使用Binary fractions包。

Example:例子:

>>> from binary_fractions import Binary
>>> b = Binary(15.5)
>>> print(b)
0b1111.1
>>>

It has many more helper functions to manipulate binary strings such as: shift, add, fill, to_exponential, invert...它有更多的辅助函数来操作二进制字符串,例如:shift、add、fill、to_exponential、invert...

PS: Shameless plug, I'm the author of this package. PS:无耻的插件,我是这个包的作者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM