String formatting: Avoid sign of a negative zero

Viewed 30

There's negative zero in Python and when formatting it to a string, the sign gets added:

>>> for value in [0.0, -0.0]:
...     f'{value:8.6f}'
... 
'0.000000'
'-0.000000'

Now I would like to have both -0.0 and 0.0 be formatted the same, without sign:

>>> for value in [0.0, -0.0]:
...     if value == -0.0: value = 0.0
...     f'{value:8.6f}'
... 
'0.000000'
'0.000000'

Success! But my code seems a bit clunky (and I'm not sure about the performance hit), is there a better way?

0 Answers
Related