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
Hello,

wondering if anybody has some experience with prices + tax calculation?
I've been dealing with a following problem:

I am supposed to display product prices in cart already with tax included, but
let's say a product price without tax:
quantity = 1
price_without_tax = 285.07
tax = 1.21    # 21%
so that means I should display product price with tax:
price_with_tax = price_without_tax * quantity * tax     # 344.93469999999996 
price_with_tax_rounded = Decimal(price_with_tax).quantize(Decimal('.01'), rounding=ROUND_UP)     # 344.94
So the price I display for product is 344.94. The problem is that if I do the same calculation for
two products:
quantity = 2
total = price_without_tax * quantity * tax     #  689.8693999999999
I would like to get to get to price = 689.88 which would make sense,
but rounding gets me to 689.87 which I guess can be confusing for customer. Also I need to add tax
on the "total price" and not to every product individually because, sometimes I need to do some
discount subtractions on total_price without tax.

Any tips?

Thanks in advance
(Sep-20-2019, 04:14 PM)mlieqo Wrote: [ -> ]I would like to get to get to price = 689.88 which would make sense,
Why would you think it is suppose to be 689.88?
if price * tax * amount is equal to 689.8693999999999 (rounded up to 689.87) then how could you get it up one more cent?

(Sep-20-2019, 04:14 PM)mlieqo Wrote: [ -> ]sometimes I need to do some
discount subtractions on total_price without tax.
If you need to do a 2% discount on a total price without tax you would multiple .98 to the total then add the tax which you could modify to the new price.
Well it just seems strange to me, to have 2 products in cart with price f.e. 10.05,
and the result price to be 20.09 (just an example).

Quote:If you need to do a 2% discount on a total price without tax you would multiple .98 to the total then add the tax which you could modify to the new price.
that would work but I do also non-percentage discounts.
(Sep-20-2019, 07:00 PM)mlieqo Wrote: [ -> ]Well it just seems strange to me, to have 2 products in cart with price f.e. 10.05,
and the result price to be 20.09 (just an example).
Where are you getting that number? 2 * 10.05 = 20.10

The modifier that makes it any different is tax or discounts. For example your first example:
>>> 285.07 * 2
570.14
>>> 285.07 * 2 * 1.21
689.8693999999999
10.05 was an example price with tax included (including tax could cause the resulting price to be 20.09), that I would have to display in cart.

I'll just stick to my first example numbers. Let's say customer would like to order 2 products, with one having price with tax 344.94 (that is the price I am supposed to display). After adding two products with price 344.94 the total price will be 688.87 (which can be odd for customer as he's probably expecting 344.94 * 2 to be the resulting price).

And that is not the case as I have to calculate the total price without the tax, substract any existing discounts (not only percentages) and then again calculate tax and sum it together -> which in this case will give me 688.87 .
(Sep-20-2019, 07:53 PM)mlieqo Wrote: [ -> ]After adding two products with price 344.94 the total price will be 688.87
No?
>>> 344.94 * 2
689.88
And your first code 689.8693999999999 can never be 689.88 without cheating adding(using Decimal module want help),which should not be done when calculation money.

For Financial calculation can also use Decimal for calculation to get correctly-rounded floating point calculation.
If someone type 1.1 * 3.5 on Phone calc they expect 3.85
>>> 1.1 * 3.5
3.8500000000000005
>>> from decimal import Decimal

>>> price = Decimal('1.1') * Decimal('3.5')
>>> price
Decimal('3.85')

>>> print(f'The price of Apple is {price}')
The price of Apple is 3.85
>>> n = 689.8693999999999
>>> print(f'The price of Shoes is {n:.2f}')
The price of Shoes is 689.87

>>> print(f'The price of Shoes is {n:.1f}')
The price of Shoes is 689.9

# Cheat
>>> n = 689.8693999999999
>>> n = n + 0.01
>>> print(f'The price of Shoes is {n:.2f}')
The price of Shoes is 689.88
>>> 344.94 * 2
689.88
(Sep-20-2019, 09:19 PM)navidmo Wrote: [ -> ]
>>> 344.94 * 2
689.88

No it's not 689.88, because 344.94 is a price with tax. If I want to calculate a total price:
285.07 * 2 * 1.21
(Sep-20-2019, 09:31 PM)mlieqo Wrote: [ -> ]No it's not 689.88, because 344.94 is a price with tax. If I want to calculate a total price:
285.07 * 2 * 1.21
That's no the same as you posting 344.94 * 2 and it can not be round to 689.88 without cheating as explained.
>>> n = 285.07 * 2 * 1.21
>>> n
689.8693999999999

>>> round(n, 3)
689.869
>>> round(n, 2)
689.87
>>> round(n, 1)
689.9
There are basic rules of rounding you can try online Rounding Numbers Calculator .
What I posted:
Quote:which can be odd for customer as he's probably expecting 344.94 * 2 to be the resulting price
which does not imply I am calculating total price with 344.94 * 2, but that it is something a customer shopping there would be expecting I assume.

I know it cannot be rounded like this, I am just asking for some work arounds when calculating prices including taxes, because I have not noticed shops having a problem like this.
Pages: 1 2