I'm printing floats trimmed to 3 digits after the zero, and I'd like to know if I can conditionally append a + before positive numbers, so I get
+0.005
for a positive change and
-0.005
for a negative change. Is this easily possible from within printf?
Yes, just use a '+' in the format specifier.
Ex:
>>> "{0:+.03f}".format(1.23456)
'+1.235'
There's a +
modifier for numbers. Eg. "%+d" or "%+f"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With