I am trying to solve the following question:
Suppose the cover price of a book is $24.95, but bookstores get a 40% discount. Shipping costs $3 for the first copy and 75 cents for each additional copy. What is the total wholesale cost for 60 copies?
Old attempt with reference to Nic3500:
book = 24.95
discount_percentage = 0.4
shipping = 3.0
reduced_shipping = 0.75
quantity = 60
discount = book * discount_percentage
print(discount)
wholesale_price = book - discount
print(wholesale_price)
total_books = wholesale_price * quantity
print(total_books)
total_shipping = shipping + (reduced_shipping * quantity)
print(total_shipping)
cost = total_books + total_shipping
print('$', cost)
New attempt using decimal with reference to Michael Butscher:
from decimal import *
getcontext().prec = 2
book = Decimal(24.95)
discount_percentage = Decimal(0.4)
shipping = Decimal(3.0)
reduced_shipping = Decimal(0.75)
quantity = Decimal(60)
discount = book * discount_percentage
print(discount)
wholesale_price = book - Decimal(discount)
print(wholesale_price)
total_books = Decimal(wholesale_price) * quantity
print(total_books)
total_shipping = shipping + (reduced_shipping * (quantity - 1))
print(total_shipping)
cost = Decimal(total_books) + Decimal(total_shipping)
print('$', cost)
The problem however is the answer should be $945.45. Due to the issues with floating point numbers somewhere in the calculation I receive the wrong answer and use that. I have looked up using the decimal module but don't understand how I would apply it to my problem, any help is appreciated, thanks.
0.4byDecimal('0.4')and keep the calculations as they are.Decimaland ran into issues, you should show what you tried and ask that question!