Today i noticed following issue on IronPython2.7:
When formatting a format_string like so, i get a "negativ zero". I get the same result if i use Python2.7 or Python3.4 instead of IronPython.
>>> "%.2f" % -0.004
'-0.00'
Then i realized, that i can get rid of the negativ zero by passing two arguments to the format string like so:
>>> "%s%.2f" % ('x', -0.004)
'x0.00'
'x' is a random character. So, i format another string before the float value and the minus disappears. That does not work with Python2.7 and Python3.4 though, where i get:
>>> "%s%.2f" % ('x', -0.004)
'x-0.00'
Does anybody know what's going on here?
What is the purpose of the formatted "negativ zero" at all?
(edited to take @MarkDikinson comment into account)
You can format the number first, then doing the
printf
-like formatting:The
+0
is there to remove the negative zero sign.round
can also take andigits
argument, to specify how many digits you need for the rounding of the number itself (not talking about display, which is handled by"%.2f"
).