2017-02-16 63 views
-1

我正在製作一個POS系統,但無論我做什麼,我都無法將錢顯示爲2位小數。Python 3.5小數位置

如果您能回覆,我將非常感激,因爲我一直在這裏待了好幾個小時。我接受所有答覆。我的代碼如下

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() 
+0

https://pyformat.info – cdarke

回答

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是一個很好的參考。

+1

非常感謝 –