简体   繁体   English

如何打印小数点后两位?

[英]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.我正在使用 python 2.75,我的工作是打印出我的咖啡馆的收据,但我不知道如何将我的货币价格打印到 2 个小数位,因为它只会打印到 1 个小数位。 I'm really new to python so if anyone to help then it'll be greatly appreciated!我对python真的很陌生,所以如果有人提供帮助,我将不胜感激!

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.并且, .2f指定了所需的格式。

You can find more on string formatting in the docs .您可以在docs 中找到有关字符串格式的更多信息。

More Pythonic way of doing this would be to use str.format() as specified by @sortfiend这样做的更多Pythonic方法是使用str.format()指定的 str.format str.format()

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

You can use printf-style formatting to output floats:您可以使用 printf 样式的格式来输出浮点数:

>>> "%.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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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