Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decimal Rounding error
#3
Thanks for your help. I fixed the issue by rounding s_discount in a separate function, and used a similar method in the additional discount function. I was finally able to separate the raw data from the display data.

"""**********
Functions
"""
#func 1: student discount which discounts the current price to 10%.
def student_discount(price):
    s_discount = price * .9  # represents 10% discount
    return s_discount #returns raw price


# used for display only, receives raw student discount
def student_rounding_display(display_price):
    display_price = round(display_price, 2) #round to 2 decimals
    display_price = "${fprice}".format(fprice=display_price)  # takes data type in format and auto-coverts the return as a string, joins with manual inserted $
    return display_price


#func 2: additional discount for regular buyers which discounts an additional 5% on the current student discounted price
# receives raw student discount, and prints display 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
    display_price = round(display_price,2) #round to 2 decimals
    display_price = "${fprice}".format(fprice=display_price)  # takes data type in format and auto-coverts the return as a string
    print(">> Total discount = ", display_price)
    #return total_discount   <--uncomment only if you'd need to use and return the non-rounded varaible, which you don't in this problem

"""**********
Main code
"""

#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(">> Student discount price = ", student_rounding_display(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
    additional_discount(s_discount)
else:
    print(">> No further discounts, final price was student-only discount: ", student_rounding_display(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,467 Feb-21-2023, 08:13 PM
Last Post: joseph202020
  from numpy array to csv - rounding SchroedingersLion 6 2,412 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  Random data generation sum to 1 by rounding juniorcoder 9 3,636 Oct-20-2021, 03:36 PM
Last Post: deanhystad
  Rounding issue kmll 1 1,491 Oct-08-2021, 10:35 AM
Last Post: Yoriz
  Not rounding to desired decimal places? pprod 2 2,638 Mar-05-2021, 11:11 AM
Last Post: pprod
  rounding and floats Than999 2 3,234 Oct-26-2020, 09:36 PM
Last Post: deanhystad
  Rounding to the nearest eight wallgraffiti 2 2,174 Jul-15-2020, 06:05 PM
Last Post: wallgraffiti
  rounding question DPaul 16 5,890 Apr-12-2020, 02:30 PM
Last Post: DPaul
  price + tax rounding mlieqo 11 6,691 Sep-21-2019, 04:53 PM
Last Post: mlieqo
  rounding floats to a number of bits Skaperen 2 2,388 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