Most popular tags

304 questions

412 answers

70 comments

329 users

Most active Members
this week:
  1. 15boyntonc - 2 points
  2. 15husbando - 1 points
  3. 15bratbyf - 1 points

Recent Badges

100 Club
- 15arnoldr -
Verified Human
- 15arnoldr -
Nice Question
- 14scotta -
Verified Human
- 15harrisonb -
Good Question
- smartin -
Nice Question
- 14scotta -
Nice Question
- 14scotta -
Nice Question
- 14scotta -
Nice Question
- 14gregoryj -
100 Club
- 15bratbyf -
100 Club
- 15bellc -
100 Club
- 15matereker -
Verified Human
- 15bellc -
Verified Human
- 15bratbyf -
100 Club
- 15agard -
Verified Human
- 15matereker -
Verified Human
- 15agard -
100 Club
- 15gardnerj -
100 Club
- 15houghm -
100 Club
- 15almondi -
100 Club
- 15huddlestonj -
Verified Human
- 15houghm -
Verified Human
- 15almondi -
Verified Human
- 15huddlestonj -
Verified Human
- 15gardnerj -
Verified Human
- 15husbando -
100 Club
- 15wilkinsona -
100 Club
- 15wearh -
100 Club
- 15Barfields -
100 Club
- 15harrisonb -

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.
...