Most popular tags

302 questions

410 answers

70 comments

310 users

Most active Members
this week:

    how to make an answer only has 2 decimals

    0 votes
    card=input("Loyalty card?") I said yes here
    #amount spent?
    amount=float(input("Cost of shop?"))  I input 70
    #more than £50 or £100?
    if card=="yes":
        #discounting
        if amount>50: price=amount/100*90
        else: price=amount
     
    elif card=="no":
        #discounting
        if amount>100: price=amount/100*95
        else: price=amount
     
     
    #final price
     
    print("Cost is £",price)
     
     
    This gives me the answer 62.999999999999999, how do i only get 2 decimals??
    asked Oct 13, 2016 in Python by 14goldinga (710 points)

    1 Answer

    0 votes
     
    Best answer
    This can be tricky...Use can use formatting to display only 2 places of decimal using %f.


    It is better to design code that avoids the need of performing maths on floating numbers.

    Could you design the program to only use pence and that would remove the problem.
    answered Nov 13, 2016 by smartin (16,520 points)
    selected Dec 2, 2016 by 14goldinga
    Welcome to Selby Computing Q&A, where you can ask questions and receive answers from other students in Computer Science.
    ...