Python 3.5小数位

时间:2017-02-16 16:37:22

标签: python sqlite decimal

我正在建立一个POS系统,但不管我做什么,我都不能把钱显示为2位小数。

如果你能回答我几个小时以来的回答,我将非常感激。我对所有回复持开放态度。我的代码在

之下
import sqlite3
import time
from decimal import Decimal

conn = sqlite3.connect('jamesPOS.db')
c = conn.cursor()
total = float(0.00)

def creatTable():
       c.execute('CREATE TABLE IF NOT EXISTS price(product TEXT, cost TEXT, product_id TEXT)')


def dataEntry():

       print('Please type a product name')
       product = input()

       print('Please type a price')
       cost = input()


       print('please type the id for your product')
       product_id = input()

       c.execute("INSERT INTO price(product, cost, product_id) VALUES (?,?,?)", (product, cost, product_id))
       conn.commit()
       print('do you want to add another product? y/n')
       loop = input()
       if loop == 'y':
              dataEntry()
       else:
              main()
def removeProduct():
       print('Sorry this is not currently avalable :(')
       time.sleep(2)
       main()

def machine():
       global total
       print('If finished type /')
       search = input('Please type or scan the product id: ')

       if search == '/':
              print('Total to pay is:£',total)
              payment = float(input('Ammount given:£'))
              change = float(total-payment)
              change = round(change, 2)
              print('Change:£',change)
              time.sleep(3)
              total = float(0.00)
              main()
       else:

              c.execute("SELECT * FROM price WHERE product_id =?",(search,))
              for row in c.fetchall():
                     print(row[0],row[1])
              #total is at the top
              price = float(row[1])
              amount = int(input('quantity = '))
              total = float(total+price*amount)
              total = round(total,2)
              print('Your overall total is:£',total)


              machine()

def productCheck():
       search = input('Please type or scan the product id: ')
       c.execute("SELECT * FROM price WHERE product_id =?",(search,))
       for row in c.fetchall():
              print(row[0],row[1])
       time.sleep(1)
       main()

def productList():
       c.execute("SELECT * FROM price")
       for row in c.fetchall():
              print(row)
       print('press / to exit')
       leave = input()
       if leave == '/':
              main()
       else:
              productList()


def main():


       print('Welcome to James POS')
       print('What do you want to do?')
       print('option 1: Add a product')
       print('Option 2: Remove a product(currently not avalable)')
       print('option 3: Product check')
       print('option 4: Use the POS')
       print('option 5: Show all products')
       print('option 6: Exit')
       action = input()

       if action == '1':
              dataEntry()
       elif action =='2':
              removeProduct()
       elif action == '3':
              productCheck()
       elif action == '4':
              machine()
       elif action == '5':
              productList()
       elif action == '6':
              exit()
       else:
              print('Sorry something went wrong')
              main()


creatTable()              
main()      
c.close()
conn.close() 

1 个答案:

答案 0 :(得分:1)

你有这个部分:

 payment = float(input('Ammount given:£'))
 change = float(total-payment)
 change = round(change, 2)
 print('Change:£',change)

不要对您的数据进行舍入,以便将其显示为两位小数。只需要使用字符串格式化打印两位小数:

print('Change: {0:.2f}'.format(change))
# Or, old syntax:
print('Change: %.2f' % change)

如评论中所述,pyformat.info是一个很好的参考。