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>Here's the solution, explanation below:</p>

<pre><code>def ToLessThanOne(num): # Change "num" into a decimal &lt;1
    while num &gt; 1:
        num /= 10
    return num

def FloatToOctal(flo, places=8): # Flo is float, places is octal places
    main, dec = str(flo).split(".") # Split into Main (integer component)
                                    # and Dec (decimal component)
    main, dec = int(main), int(dec) # Turn into integers

    res = oct(main)[2::]+"." # Turn the integer component into an octal value
                             # while removing the "ox" that would normally appear ([2::])
                             # Also, res means result

    # NOTE: main and dec are being recycled here

    for x in range(places): 
        main, dec = str((ToLessThanOne(dec))*8).split(".") # main is integer octal
                                                           # component
                                                           # dec is octal point
                                                           # component
        dec = int(dec) # make dec an integer

        res += main # Add the octal num to the end of the result

    return res # finally return the result
</code></pre>

<p>So you can do <code>print(FloatToOctal(12.325))</code> and it shall print out <code>14.246314631</code></p>

<p>Finally, if you want less octal places (decimal places but in octal) simply add the <code>places</code> argument: <code>print(FloatToOctal(12.325, 3))</code> which returns <code>14.246</code> as is correct according to this website: <a href="http://coderstoolbox.net/number/" rel="nofollow">http://coderstoolbox.net/number/</a></p>

python binary floating-point octal