1

So I'm trying to get the dollar sign to appear directly to the left of the digits under the column 'Cost'. I'm having trouble figuring this out and any help is appreciated.

# Question 5

print("\nQuestion 5.")

# Get amount of each type of ticket to be purchased

adultTickets = input("\nHow many adult tickets you want to order: ")

adultCost = int(adultTickets) * 50.5

childTickets = input("How many children (>=10 years old) tickets: ")

childCost = int(childTickets) * 10.5

youngChildTickets = input("How many children (<10 years old) tickets: ")

# Display ticket info

print ("\n{0:<17} {1:>17} {2:>12}".format("Type", "Number of tickets", 
"Cost"))

print ("{0:<17} {1:>17}${2:>12.2f}".format("Adult", adultTickets, adultCost))

print ("{0:<17} {1:>17}${2:>12.2f}".format("Children (>=10)", childTickets, 
childCost))

print ("{0:<17} {1:>17} {2:>12}".format("Children (<10)", youngChildTickets, 
"free"))

#Calculate total cost and total amount of tickets

totalTickets = (int(adultTickets) + int(childTickets) + 
int(youngChildTickets))

totalCost = adultCost + childCost

print ("{0:<17} {1:>17}${2:>12.2f}".format("Total", totalTickets, totalCost))

I also want the cost column to be formatted right, which for some reason it isn't when I run the program.

(My output):

enter image description here Edit: I also can't format the '$' onto the cost, as I have to keep the 2 decimal places in the formatting

AMC
  • 2,642
  • 7
  • 13
  • 35
John DOe
  • 69
  • 1
  • 1
  • 7
  • Does this answer your question? [Converting Float to Dollars and Cents](https://stackoverflow.com/questions/21208376/converting-float-to-dollars-and-cents) – AMC Mar 10 '20 at 00:34
  • https://stackoverflow.com/questions/320929/currency-formatting-in-python – AMC Mar 10 '20 at 00:35
  • I think what I've done is already basically that, but for some reason it's undoing the align to right and it's causing it to look how it does. Do you have any idea why? – John DOe Mar 10 '20 at 00:38
  • Isn't that just due to the length of the text before the numbers? – AMC Mar 10 '20 at 00:41
  • I'm not too sure but it's not right aligned when I run it. If there's a way to get rid of the white space I could do that but I need to keep the '>12'. – John DOe Mar 10 '20 at 00:47
  • Can you make a [mcve]? I would like to be able to easily run the code myself. – AMC Mar 10 '20 at 00:50
  • I think the code I posted can be run to achieve what I have. I'll repost what I've updated it to, I also fixed the right-alignment thing, was just to do with spacing. – John DOe Mar 10 '20 at 00:51
  • The one thing that's missing would be hardcoded input, without it debugging/development is going to be quite annoying. – AMC Mar 10 '20 at 00:55
  • Yeah sorry, this is just literally my first assignment on python haha, I'm just getting really frustrated with this formatting thing because I can't see what's wrong. – John DOe Mar 10 '20 at 01:03

4 Answers4

8

If I understand correctly, you want to format a floating point number into a string, and have a dollar sign appear in the output in between the number and the padding that you use to space it out in the string. For example, you want to be able to create these two strings with the same formatting code (just different values):

foo:    $1.23
foo:   $12.34

Unfortunately, you can't do this with just one string formatting operation. When you apply padding to a number, the padding characters will appear in between the number and any prefixing text, like the dollar signs in your current code. You probably need to format the number in two steps. First make the numbers into strings prefixed with the dollar signs, then format again to insert the dollar strings into the final string with the appropriate padding.

Here's how I'd produce the example strings above:

a = 1.23
b = 12.34

a_dollars = "${:.2f}".format(a)  # make strings with leading dollar sign
b_dollars = "${:.2f}".format(b)

a_padded = "foo:{:>8}".format(a_dollars)  # insert them into strings with padding
b_padded = "foo:{:>8}".format(b_dollars)
Blckknght
  • 100,903
  • 11
  • 120
  • 169
0

I had the same issue, and I was able to solve it with a string like this:

print("%5d" % month,"%3s"%'$',"%12.2f" % balance,"%4s"%'$',"%11.2f" % interest,"%4s"%'$',"%13.2f" % principal)

It printed this in a nice even table.

Month | Current Balance | Interest Owned | Principal Owned

   1  $       9000.00  $       90.00  $        360.00   

This indented the $ to the start of each table column, while also leaving enough space for very large figures. It's not ideal though since it would be nice to nest it right next to the dollar number.

0

How I did this?

bill = input("What is the total bill? ")
bill_amount = int(bill)
amount_per_person = (bill_amount)/5
print("$"+"%.2f" % amount_per_person)

output

What is the total bill? 100
$22.40
0

I sometimes write my strings by inserting them from a character table for readability.

db = chr(36) # $ 
cost = 98.12
print ("Total: " + db + str(cost))

Total: $98.12

https://python-tcod.readthedocs.io/en/latest/tcod/charmap-reference.html