Computer Science - Sal's Shipping I want to Show a dollar amount to two places

I am on the computer science Sal’s Shipping project. How do I get my final dollar amount to display to 2 decimal places. i want the 2 decimals after the str(cost).

Here is the print command I am using:
print ("You should ship using " + method + ", it will cost " + “$” + str(cost) + “.”)

Link to code

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

1 Like

You’ll probably want to look into the formatting methods avilable for strings in Python. There are other ways to do this such as changing the cents value to an integer but it’s unlikely to make a difference for these calculations-

A nice guide on formatting-
https://pyformat.info/

Python documentation on string formatting
https://docs.python.org/3/library/string.html#formatstrings
https://docs.python.org/2/library/stdtypes.html#string-formatting-operations

A very quick example-
‘{:.4f}’.format(1117.35367893) # produces ‘1117.3537’

1 Like