You are seeing the default str()
conversion for floating point numbers at work. You can pick a different conversion by formatting the number explicitly.
The format()
function can do this for you:
>>> n = 9.87654312e+15
>>> format(n, 'f')
'9876543120000000.000000'
See the Format Specification Mini-Language documentation for more options. The 'f'
format is but one of several:
Fixed point. Displays the number as a fixed-point number. The default precision is 6.
The default precision resulting in the .000000
six digits after the decimal point; you can alter this by using .<precision>f
instead:
>>> format(n, '.1f')
'9876543120000000.0'
but take into account that decimals are rounded to fit the requested precision.
The g
format switches between using exponents (e
) and f
notation, depending on the size of the number, but won't include decimals if the number is whole; you could use a very large precision with 'g'
to avoid printing decimals altogether:
>>> format(n, '.53g')
'9876543120000000'
To be explicit, str(n)
is the same as format(n, '.12g')
, repr(n)
is format(n, '.17g')
; both can use the exponent format when the exponent is larger than the precision.
repr(n)
might also be an option – Luxemburg