Update: as up to now (2019-09), masking leading or trailing zeros in decimal numbers formatted to string seems to be unsupported in Python. You will need to use a workaround to get something like '.01' from the number 0.0101 (assuming 3 decimal places desired).
I would even argue that it's a good thing not to support such a format since
- I'd consider '0.01' be better in terms of readability than '.01'
- '0.010' carries information (3 digits of precision...) that is lost in '0.01'
If desired anyway, one could use one of the suggestions below. Thank you all for contributing.
Q: I'm looking for a way to output floating point numbers as strings, formatted without leading/trailing zeros. Is there a way to do this with '{ }'.format()
or f-string? I searched the internet but didn't find anything. Did I just miss it or is it not possible (Python 3.7)?
What I have in mind is basically
some_number = 0.3140
string = f'{some_number:x}' # giving '.314'
that gives the output string '.314'.
. So is there an x
that does this?
Of course one could work-around with lstrip
/ rstrip
as described e.g. here or similar here:
In [93]: str(0.3140).lstrip('0').rstrip('0')
Out[93]: '.314'
but it would be more convenient to use only an f-string. Since I can use that for other formatting options, optionally calling strip
demands additional lines of code.