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

9
  • Does this answer your question? Converting Float to Dollars and Cents Commented Mar 10, 2020 at 0:34
  • stackoverflow.com/questions/320929/… Commented Mar 10, 2020 at 0: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? Commented Mar 10, 2020 at 0:38
  • Isn't that just due to the length of the text before the numbers? Commented Mar 10, 2020 at 0: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'. Commented Mar 10, 2020 at 0:47

4 Answers 4

9

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Worked perfect. Thanks.
Excellent solution. Where did you get this from @Blckknght . Was is documented somewhere ?
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.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.