How do I convert float decimal to float octal/binary?
<p>I have been searched everywhere to find a way to convert float to octal or binary. I know about the <code>float.hex</code> and <code>float.fromhex</code>. Is theres a modules which can do the same work for octal/binary values?</p>

<p>For example: I have a float <code>12.325</code> and I should get float octal <code>14.246</code>. Tell me, how I can do this? Thanks in advance.</p>

<p>You could write your own, if you only care about three decimal places then set  n to 3:</p>

<pre><code>def frac_to_oct(f, n=4):
    # store the number before the decimal point
    whole = int(f)
    rem = (f - whole) * 8
    int_ = int(rem)
    rem = (rem - int_) * 8
    octals = [str(int_)]
    count = 1
    # loop until 8 * rem gives you a whole num or n times
    while rem and count &lt; n:
        count += 1
        int_ = int(rem)
        rem = (rem - int_) * 8
        octals.append(str(int_))
    return float("{:o}.{}".format(whole, "".join(octals)))
</code></pre>

<p>Using your input <em>12.325</em>:</p>

<pre><code>In [9]: frac_to_oct(12.325)
Out[9]: 14.2463
In [10]: frac_to_oct(121212.325, 4)
Out[10]: 354574.2463

In [11]: frac_to_oct(0.325, 4)
Out[11]: 0.2463
In [12]: frac_to_oct(2.1, 4)
Out[12]: 2.0631
In [13]:  frac_to_oct(0)
Out[13]: 0.0
In [14]:  frac_to_oct(33)
Out[14]: 41.0
</code></pre>

python binary floating-point octal