Reputation: 453
So I am having this problem with the Python formatting where the sign in front of a number is taking a space up and is misaligning all of the numbers I want to present. In addition, the positive signs are not showing up as they should be.
Here is an example of my code:
number1 = 23.12312312
number2 = -31.3131313
number3 = 63.1335
number4 = 12.323
number5 = 23.1111
number6 = 14.5555
print("{0:<15} {1:+>3.6f} {2:+>3.6f}".format(number1, number2, number3))
print("{0:<15} {1:+>3.6f} {2:+>3.6f}".format(number4, number5, number6))
print("{0:<15} {1:+>3.6f} {2:+>3.6f}".format(number1, number2, number3))
Output:
23.12312312 -31.313131 63.133500
12.323 23.111100 14.555500
23.12312312 -31.313131 63.133500
Is there any way to fix this?
Upvotes: 1
Views: 164
Reputation: 70582
Let's just pick on one number. Study these until the light dawns ;-)
>>> x = 23.12312312
>>> print "{:3.6f}".format(x)
23.123123
>>> print "{:10.6f}".format(x)
23.123123
>>> print "{:11.6f}".format(x)
23.123123
>>> print "{:+11.6f}".format(x)
+23.123123
The number before the .
is the total width of the output field, not the number of digits before the decimal point. Your 3
is way too small.
And there's usually no need for >
- most things right-justify by default. If you have to use it, put the +
after >
, not before >
. Good enough?
Upvotes: 4