python decimal print all internal decimal places
Question: i am looking for a way to print all internal decimal places of a python
decimal. has anyone an idea how to achieve following. The example code is
written in Python.

    
    
    from decimal import *
    bits = 32
    precision = Decimal(1) / Decimal(2**bits)
    val = decimal(1078947848)
    

what happens now for following if i multiply val * precision i get following
result

    
    
    val * precision
    Decimal('0.2512121219187974929809570312')
    

but i know the last number 2 is not the end of my number, so i can do
following which gives me this result

    
    
    "%.100f" % x
    '0.2512121219187974929809570312500000000000000000000000000000000000000000000000000000000000000000000000'
    

however all the trailing zeros are not desired, and the size of %.100f depends
on the given bits. It should be simple math and maybe I should give myself a
break, but how can I derive the number of internal decimal places from the
given number of bits 32.


Answer: From the [documentation](https://docs.python.org/2/library/decimal.html):

> the decimal module has a user alterable precision (defaulting to 28 places)
> which can be as large as needed for a given problem

Your number is 29 digits long, so it's just a little too much for the default
precision. Try increasing it. 29 would work for this instance, but you may
wish to choose a larger number if you don't know how many decimal places
you'll need later.

    
    
    from decimal import *
    getcontext().prec = 100
    bits = 32
    precision = Decimal(1) / Decimal(2**bits)
    val = Decimal(1078947848)
    print val * precision
    

Result:

    
    
    0.25121212191879749298095703125000
    

Also, you can strip those trailing zeroes with a call to `normalize`.

    
    
    >>> print (val * precision).normalize()
    0.25121212191879749298095703125
    

