Bottom Page

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.
mlieqo likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#12
Thanks for the help! :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  rounding floats to a number of bits Skaperen 2 249 Sep-13-2019, 04:37 AM
Last Post: Skaperen
  Stock Rate of Change (based on open and current price) Not Working firebird 1 262 Jul-29-2019, 07:10 AM
Last Post: perfringo
  str.format rounding to the left of the decimal ClassicalSoul 2 411 Mar-27-2019, 11:12 AM
Last Post: perfringo
  Rounding/Truncating Numbers Trinx 3 556 Jan-16-2019, 10:54 PM
Last Post: Trinx
  Weird rounding PythonZenon 8 872 Oct-12-2018, 09:22 PM
Last Post: volcano63
  Controlling trailing zeros with rounding? RedSkeleton007 1 9,120 Jan-25-2018, 07:23 AM
Last Post: j.crater
  Date for Parking price PyQt5 vbv 0 1,344 Apr-19-2017, 08:53 PM
Last Post: vbv
  rounding locale.currency birdieman 8 4,234 Dec-28-2016, 01:48 AM
Last Post: birdieman

Forum Jump:


Users browsing this thread: 1 Guest(s)