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)
endswith
instead of.find < 0
, but other than that it looks fine. This would probably be better for codereview, as "more elegantly" isn't really something objective that we can give you. - Morgan Thrappendswith
help with this problem? - Selcuk'.' in out
- Jean-François Fabre