简体   繁体   中英

How to print in 2 decimal places?

I'm using python 2.75 and my job is to print out a receipt of my cafe but I don't know how to print out my prices in currency value to 2 decimal places as it'll just print out to 1 decimal places. I'm really new to python so if anyone to help then it'll be greatly appreciated!

def main():
print("Welcome to Kenny's Drinking Cafe")
print("Here is the electronic menu: ")
print("Please select by typing the selections from the list below. *CaSe InSeNsItIvE*")
print("Spelling counts though ;)")
print "-------------------------------------------------------------------------------"
print("Coffee   $2.00")
print("Hot Chocolate   $1.50")
print("Latte   $3.00")
print("Frappuccino   $3.00")
print("Smoothie   $3.50")
countcoffee = 0
counthotc = 0
countlatte = 0
countfrap = 0
countsmoothie = 0 
coffeec = 2.00
hotcc = 1.50
lattec = 3.00
frapc = 3.00
smoothiec = 3.50

order1 = raw_input("Please enter your selection: Press Enter to Print receipt ")
while order1 != "":
    order1 = order1.lower()
    if order1 == str("coffee"):
        countcoffee = countcoffee + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    elif order1 in [str("hot chocolate"), str("hotchocolate")]:
        counthotc = counthotc + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    elif order1 == str("latte"):
        countlatte = countlatte + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    elif order1 == str("frappuccino"):
        countfrap = countfrap + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    elif order1 == str("smoothie"):
        countsmoothie = countsmoothie + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    else:
        print "Please type correctly"
        exit()

coffee = (countcoffee*coffeec)
hotchocolate = (counthotc*hotcc)
latte = (countlatte*lattec)
frappuccino = (countfrap*frapc)
smoothie = (countsmoothie*smoothiec)
sum = (countcoffee*coffeec) + (counthotc*hotcc) + (countlatte*lattec) + (countfrap*frapc) + (countsmoothie*smoothiec)
print ""
print "Kenny's Drinking Cafe Receipt"
print ""
print "***********************"
print "**   Regular Items   **"
print "***********************"
if coffee > 0:
    print (str(countcoffee) + ' coffee ' + "@ $" + str(coffee))
if hotchocolate > 0:
    print counthotc,"hot chocolate","@ $",hotchocolate
if latte > 0:
    print countlatte,"latte", "@ $",latte
if frappuccino > 0:
    print countfrap,"frappuccino","@ $",frappuccino
if smoothie > 0:
    print countsmoothie,"smoothie","@ $",smoothie
print ("BALANCE: $" + str(sum))

main()

This should help

print("BALANCE: $%.2f" % sum)

The % operator replaces supplied parameter in the string. And, .2f specifies the required format.

You can find more on string formatting in the docs .

More Pythonic way of doing this would be to use str.format() as specified by @sortfiend

print("BALANCE: {:.2f}".format(sum))

You can use printf-style formatting to output floats:

>>> "%.2f" % 1
'1.00'
>>> "%.2f" % 1.0
'1.00'
>>> "%.2f" % 1.03
'1.03'
>>> "%.2f" % 1.034
'1.03'

You can use the same expression to assign to a variable:

>>> str = "%.2f" % 1.035
>>> str
'1.03'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM