Jan-02-2021, 06:46 PM
Hi,
I've got a code written where the purpose is to take a raw price value and apply 10% discount to it. If the user agrees, then a 2nd discount of 5% is added as an additional discount to the previous discount price. If not, the program exits.
The code works, but I'm having difficulties with rounding the prices to 2 decimals. In the first function, the price correctly displays 2 decimals, e.g. if I enter 85.49, the function will display $76.94 as the 10$ discount price.
However, the returned value from this function will be 76.941. When this is then passed into teh 2nd function for applying the 5% discount, the function displays $73.09394999999999, and returns $73.09394999999999
Any ideas?
I've got a code written where the purpose is to take a raw price value and apply 10% discount to it. If the user agrees, then a 2nd discount of 5% is added as an additional discount to the previous discount price. If not, the program exits.
The code works, but I'm having difficulties with rounding the prices to 2 decimals. In the first function, the price correctly displays 2 decimals, e.g. if I enter 85.49, the function will display $76.94 as the 10$ discount price.
However, the returned value from this function will be 76.941. When this is then passed into teh 2nd function for applying the 5% discount, the function displays $73.09394999999999, and returns $73.09394999999999
Any ideas?
from decimal import Decimal #func 1: student discount which discounts the current price to 10%. def student_discount(price): s_discount = price * .9 # represents 10% discount display_price = s_discount num = Decimal(s_discount) display_price = round(num,2) display_price = "${fprice}".format(fprice=display_price) # takes data type in format and auto-coverts the return as a string print(">> Your new price = ", display_price) return s_discount #func 2: additional discount for regular buyers which discounts an additional 5% on the current student discounted price def additional_discount(s_discount): total_discount = s_discount * .95 #represents a 5% discount on the already reduced 10% discount display_price = total_discount num = Decimal(total_discount) display_price = round(num,2) display_price = "${fprice}".format(fprice=total_discount) # takes data type in format and auto-coverts the return as a string print(">> Total discount = ", display_price) return total_discount #enter retail value of a product price = float(input("Enter price: ")) #pass raw price into func 1 for 10% reduction s_discount = student_discount(price) print("s_discount = ",s_discount) #ask user if further 5% should be included user_input = input("\nInclude another 5% discount? Press 'y' or 'n': ") if user_input == 'y': # pass the 10% price reduction outcome into func 2 for further 5% reduction total_discount = additional_discount(s_discount) print("Total_discount = ", total_discount) else: print("No further discounts, final price was student-only discount: ", "${fprice}".format(fprice=s_discount))