2017-02-16 17 views
-1

私はPOSシステムを作っていますが、私は何をしても、小数点以下2桁まで表示することはできません。小数点以下の桁数

私がこれに何時間もいたように返信できるなら、非常に感謝します。私はすべての回答にオープンしています。

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

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

https://pyformat.info – cdarke

答えて

1

を下回っています。ただ、それは文字列の整形を使用して、2つの小数で印刷するように依頼してください:コメントで述べたように

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

pyformat.infoは良い参考です。

+1

ありがとうございます –

関連する問題