0

I am trying to right align some monetary values to look like this:

John Smith         $3,500
Jane Doe             $200
Jack Johnson         $700

But I am getting this:

John Smith         $3500
Jane Doe           $ 200
Jack Johnson       $ 700.

My code for the output looks like this:

for (name, amount) in cursor:
    print("{}".format(name)+ "\t\t\t" + "${:>5,.0f}".format( amount))

I would also like to put a comma separator for numbers in the thousands. I know I've seen the formatting for this before, but I am having trouble finding it again.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
ethanm1
  • 15
  • 4
  • I don't think there's a built in directive for that, so you might have to figure out how long the number is for yourself and write your own function to print it. – kqr Nov 12 '15 at 06:50
  • 1
    http://stackoverflow.com/questions/5513615/add-thousands-separators-to-a-number – Jiayu Wang Nov 12 '15 at 06:50

2 Answers2

0

Try this, remove the spaces at both sides of the number string.

for (name, amount) in cursor: 
    print("{}".format(name)+ "\t\t\t" + "{:>5}".format("$"+ "{:.0f}".format(amount).strip()))
Xiaoqi Chu
  • 1,497
  • 11
  • 6
-1

Try this

for (name,amount) in cursor:
     print("{}".format(name) + "\t\t\t"  +  "${}".format(int(amount)))
Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
  • That solves the $ being next to the number problem, but I don't need the decimals after the numbers. – ethanm1 Nov 12 '15 at 06:54
  • Do you realize that `'{}'.format(name)` is kind of pointless? And `'${}'.format(int(amount))` doesn't right-align anything - and the `int()` call is unnecessary? This isn't even an attempt at solving the problem. – TigerhawkT3 Nov 12 '15 at 08:08