Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
price + tax rounding
#11
Example with commercial round
Usually scientific rounding is used.


def calc_tax(price, tax_in_percent):
    with decimal.localcontext() as ctx:
        ctx.rounding = decimal.ROUND_05UP # commercial round
        price = decimal.Decimal(price, ctx)
        tax = decimal.Decimal(tax_in_percent, ctx) / 100
        return price * tax

        
def get_input():
    price = input('Price: ')
    tax = input('Tax (in %): ')
    result = calc_tax(price, tax)
    print(result)
    # you can work with result, but
    # before you print results, round them
    print(round(result, 2))
You can also create a context with decimal.Context(), make your changes on the context and set the context with decimal.setcontext(ctx) as global context.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#12
Thanks for the help! :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  need help rounding joseph202020 7 1,317 Feb-21-2023, 08:13 PM
Last Post: joseph202020
  from numpy array to csv - rounding SchroedingersLion 6 2,163 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  Finding the price based on industry and number of transactions chandramouliarun 0 910 Jul-26-2022, 07:36 PM
Last Post: chandramouliarun
  class - calculate total price of copies 3lnyn0 1 1,554 Dec-12-2021, 11:43 PM
Last Post: BashBedlam
  Random data generation sum to 1 by rounding juniorcoder 9 3,412 Oct-20-2021, 03:36 PM
Last Post: deanhystad
  Rounding issue kmll 1 1,410 Oct-08-2021, 10:35 AM
Last Post: Yoriz
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 3,164 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Not rounding to desired decimal places? pprod 2 2,545 Mar-05-2021, 11:11 AM
Last Post: pprod
  Decimal Rounding error project_science 4 2,751 Jan-06-2021, 03:14 PM
Last Post: project_science
  An important question is how to create a zigzag in price data? epsilon 0 1,297 Nov-18-2020, 08:06 PM
Last Post: epsilon

Forum Jump:

User Panel Messages

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