Skip to content Skip to sidebar Skip to footer

How To Print Currency Symbol And An N-digit Decimal Number Right Aligned In Python

I'm making an EMI calculator, which displays an amortization table after displaying monthly EMI. How can I right align the currency symbol and any n-digit decimal number? I tried t

Solution 1:

Extending the previous answer a little:

rupee = u'\u20B9'
amounts = [12345.67, 1.07, 22.34, 213.08, 4.98]

for amount in amounts:
    print('{:>10}'.format(rupee + '{:>.2f}'.format(amount)))

Output:

 ₹12345.67
     ₹1.07
    ₹22.34
   ₹213.08
     ₹4.98

Solution 2:

If you know the maximum number of characters in your output, then you can do something like the following. See Format Specification Mini-Language for the various available format specifiers.

amounts = ['$1.07', '$22.34', '$213.08', '$4.98']

for amount in amounts:
    print('{:>8}'.format(amount))

# OUTPUT#   $1.07#  $22.34# $213.08#   $4.98

Post a Comment for "How To Print Currency Symbol And An N-digit Decimal Number Right Aligned In Python"