Python v2.6 rounding up with decimals (currency)?
Question: So I am very new to Python and have a question about rounding up.

    
    
    product_price = '79.98'
    subtotal = Decimal(product_price)
    cal_tax = '0.0825'
    tax_conv = Decimal(cal_tax)
    tax_total = subtotal * tax_conv
    total_tax = round(tax_total, 2)
    print total_tax
    self.assertEqual(total_tax, '6.60')
    

total_tax returns `6.6`, and the assert fails:

    
    
    AssertionError: u'6.60' != 6.5999999999999996
    

I am sure there is a newbie mistake I am making. I would like for 'total_tax'
in this instance to round to 6.60 passing my assertion. I would be grateful
for any tips anyone has to offer. Thanks again!


Answer: Do not use `round` on decimals, `round` returns `float`. Use `quantize`:

    
    
    total_tax = tax_total.quantize(Decimal('0.01'))
    

Then compare it with another `Decimal`:

    
    
    self.assertEqual(total_tax, Decimal('6.60'))
    



If you want to round strictly up, quantize like this:

    
    
    from decimal import Decimal, ROUND_UP
    total_tax = tax_total.quantize(Decimal('0.01'), rounding=ROUND_UP)
    

