Reputation: 23
I am trying to right align the numerical values which I get from print, but I am getting an error:
'float' object has no attribute 'rjust'
Here is my code where I am trying to use it at. How should I go about right aligning all the numerical values I get with print
:
enterValue = float(input("The input value is: "))
# We know that the circumference of a circle is 2*pi*r.
CircumferenceV = (2*math.pi*enterValue)
print("The Circumference is : %0.2f" %(circleCircumference.rjust(10)))
Upvotes: 0
Views: 191
Reputation: 160667
You're applying rjust
on a float
when it is defined for str
objects. An alternative is to right align with the :fill.precision
notation on format
:
>>> print("{:10.2f}".format(3.142059))
3.14
Applied on circleCircumference
:
print("The Circumference is : {:10.2f}".format(circleCircumference))
Using only %
one can do the same thing with:
>>> circleCircumference = 3.29920
>>> "Circle circumfence: %10.2f" % circleCircumference
'Circle circumfence: 3.30'
Upvotes: 2
Reputation: 81
If you'd only like 2 decimal places, you can use the same answer that Jim suggested but modify it slightly.
print("The Circumference is : {:>10.2f}".format(circleCircumference))
This tell the print statement to print circleCircumference to 2 places as a float, while right justifying it in a 10 character column.
Upvotes: 1