How to have negative zero always formatted as positive zero in a python string?
Question:
I have the following to format a string:
'%.2f' % n
If n
is a negative zero (-0
, -0.000
etc) the output will be -0.00
.
How do I make the output always 0.00
for both negative and positive zero values of n
?
(It is fairly straight forward to achieve this but I cannot find what I would call a succinct pythonic way. Ideally there is a string formatting option that I am not aware of.)
Answers:
import re
re.sub("[-+](0.0+)", r"1", number)
e.g.:
re.sub("[-+](0.0+)", r"1", "-0.0000") // "0.0000"
re.sub("[-+](0.0+)", r"1", "+0.0000") // "0.0000"
Add zero:
>>> a = -0.0
>>> a + 0
0.0
which you can format:
>>> '{0:.3f}'.format(a + 0)
'0.000'
>>> x= '{0:.2f}'.format(abs(n) if n==0 else n)
>>> print(x)
0.00
reason for the if
condition:
>>> -0.0000==0
True
>>> 0.000==0
True
>>> 0.0==0
True
The most straightforward way is to specialcase zero in your format:
>>> a = -0.0
>>> '%.2f' % ( a if a != 0 else abs(a) )
0.0
However, do note that the str.format
method is preferred over %
substitutions – the syntax in this case (and in most simple cases) is nearly identical:
>>> '{:.2f}'.format(a if a != 0 else abs(a))
Also note that the more concise a or abs(a)
doesn’t seem to – even though bool(a)
is False
.
A very closely related problem is that -0.00001 is also formatted as “-0.00”. That can be just as confusing. The answers above will not take care of this (except for user278064’s, which needs an anchored regexp).
It’s ugly, but this is the best I can do for this case:
import re
re.sub (r"^-(0.?0*)$", r"1", "%.2f" % number)
I have the following to format a string:
'%.2f' % n
If n
is a negative zero (-0
, -0.000
etc) the output will be -0.00
.
How do I make the output always 0.00
for both negative and positive zero values of n
?
(It is fairly straight forward to achieve this but I cannot find what I would call a succinct pythonic way. Ideally there is a string formatting option that I am not aware of.)
import re
re.sub("[-+](0.0+)", r"1", number)
e.g.:
re.sub("[-+](0.0+)", r"1", "-0.0000") // "0.0000"
re.sub("[-+](0.0+)", r"1", "+0.0000") // "0.0000"
Add zero:
>>> a = -0.0
>>> a + 0
0.0
which you can format:
>>> '{0:.3f}'.format(a + 0)
'0.000'
>>> x= '{0:.2f}'.format(abs(n) if n==0 else n)
>>> print(x)
0.00
reason for the if
condition:
>>> -0.0000==0
True
>>> 0.000==0
True
>>> 0.0==0
True
The most straightforward way is to specialcase zero in your format:
>>> a = -0.0
>>> '%.2f' % ( a if a != 0 else abs(a) )
0.0
However, do note that the str.format
method is preferred over %
substitutions – the syntax in this case (and in most simple cases) is nearly identical:
>>> '{:.2f}'.format(a if a != 0 else abs(a))
Also note that the more concise a or abs(a)
doesn’t seem to – even though bool(a)
is False
.
A very closely related problem is that -0.00001 is also formatted as “-0.00”. That can be just as confusing. The answers above will not take care of this (except for user278064’s, which needs an anchored regexp).
It’s ugly, but this is the best I can do for this case:
import re
re.sub (r"^-(0.?0*)$", r"1", "%.2f" % number)