python float to string without precision loss
Question: For python 3 I want to convert a float to a string, with possibly different
length (i.e. number of digits) but with full precision.

Also I need to have a decimal point in any case:

    
    
    1    -> '1.'
    1/10 -> '0.1000000000000000055511151231257827021181583404541015625'
    

currently my code is this:

    
    
    from decimal import Decimal
    def formatMostSignificantDigits(x):
        out = str(Decimal(x))
        if out.find('.') < 0:
            out += '.'
        return out
    

can this be done more elegantly? (`e` notation would be possible, too)


Answer: Use Pythons [string formatting
functions](https://docs.python.org/3/library/string.html#format-examples):

    
    
    >>> x = 1.0; '{:.55f}'.format(x)
    '1.0000000000000000000000000000000000000000000000000000000'
    >>> x = 1/10; '{:.55f}'.format(x)
    '0.1000000000000000055511151231257827021181583404541015625'
    

If you want to be able to feed it integers (such as `1`) as well, use
`'{:.55f}'.format(float(x))`.

If you want to strip any trailing zeroes, use
`'{:.55f}'.format(x).rstrip('0')`.

Note that 55 decimals after the point is way overkill (but it's what you
showed in your question); 16 digits should suffice to express the full
precision of double-precision IEEE 754 floats (20 digits for the 80-bit
extended-precision you might encounter).

