Python Forum

Full Version: price + tax rounding
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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.
Thanks for the help! :)
Pages: 1 2