Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decimal Rounding error
#1
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?

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))
Reply


Messages In This Thread
Decimal Rounding error - by project_science - Jan-02-2021, 06:46 PM
RE: Decimal Rounding error - by deanhystad - Jan-02-2021, 07:15 PM
RE: Decimal Rounding error - by project_science - Jan-03-2021, 07:01 PM
RE: Decimal Rounding error - by deanhystad - Jan-03-2021, 09:34 PM
RE: Decimal Rounding error - by project_science - Jan-06-2021, 03:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  need help rounding joseph202020 7 1,342 Feb-21-2023, 08:13 PM
Last Post: joseph202020
  from numpy array to csv - rounding SchroedingersLion 6 2,214 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  Random data generation sum to 1 by rounding juniorcoder 9 3,464 Oct-20-2021, 03:36 PM
Last Post: deanhystad
  Rounding issue kmll 1 1,428 Oct-08-2021, 10:35 AM
Last Post: Yoriz
  Not rounding to desired decimal places? pprod 2 2,580 Mar-05-2021, 11:11 AM
Last Post: pprod
  rounding and floats Than999 2 3,127 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  Rounding to the nearest eight wallgraffiti 2 2,100 Jul-15-2020, 06:05 PM
Last Post: wallgraffiti
  rounding question DPaul 16 5,660 Apr-12-2020, 02:30 PM
Last Post: DPaul
  price + tax rounding mlieqo 11 6,509 Sep-21-2019, 04:53 PM
Last Post: mlieqo
  rounding floats to a number of bits Skaperen 2 2,324 Sep-13-2019, 04:37 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020