2

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?

FriendFX
  • 2,929
  • 1
  • 34
  • 63
  • 4
    By the way, it seems that python 3.11 will introduce `z` format specifier to address this issue: https://peps.python.org/pep-0682/ and https://discuss.python.org/t/accepting-pep-682-format-specifier-for-signed-zero/14088 – j1-lee Jun 07 '22 at 02:37

0 Answers0