I'm looking for a way to output floating point numbers as strings, formatted without leading zeros. Is there a way to do this with '{ }'.format()
? I searched the internet but didn't find anything. Did I just miss it or is it impossible?
What I have in mind is basically
some_number = 0.314
string = '{x}'.format(some_number)
that gives the output string '.314'.
. The task is: find x.
Of course one could do this with lstrip
as described e.g. here or similar here:
In [93]: str(0.314).lstrip('0')
Out[93]: '.314'
but it would be more convenient to use only the '{ }'.format()
method in my opinion. Since I can use that for other formatting options, optionally calling lstrip
demands additional lines of code.
if you want to just strip 0 from floating numbers you could use this "hack"
this is in no way an elegant solution, but it will get the job done
also if you want to use .format as well you don't need another line, you could just
If you want to use
format()
, then try as below.Just use
lstrip()
within format function, you don't need to write additional line of code.