I dont know why but you can fix it explicitly using Decimal.
http://docs.python.org/library/decimal.html#module-decimal

2012/3/5 Francisco <[email protected]>:
> Hi everyone. I just came across a weird behavior and I'm wondering if it's
> some kind of bug or just my fault.
>
> I have a model for a product like this:
>
> db.define_table('products',
>     Field('name',length=128, notnull=True),
>     Field('sale_price',type='decimal(14,4)',default=0, notnull=True))
>
> Now when the user adds a product to the cart I'm storing the row for the
> product in a dictionary in the session like this:
>
> session.cart[product.id] = product
>
> finally the view that displays it lets you pick among several payment
> options and based on the option it multiplies the price by another number
> like this:
>
> {{=session.cart[id].sale_price * session.payment_factor}}
>
> Both sale price and payment factor are store in DB as decimal(14,4). The
> first time the code is run the view executes correctly. But when you add
> another product to the cart, or modify any other thing that requires a
> reload or repost of the page I get an error like this:
>
> TypeError: unsupported operand type(s) for *: 'float' and 'Decimal'
>
>
> So what is happening is that after the first time my variable which is store
> in the session and that should be treated like a Decimal is being returned
> as a float. This is happening in the development environment with SQLite, I
> have not tested this yet under MySQL. I think it is caused by the fact that
> SQLite stores decimals as float, buy I wonder why does the first time it is
> transformed to Decimal, and then just used as a float? And I wonder if I
> must manually converted them to Decimal every time I use this variables.
>
> Hope someone can help.



-- 
Carlos J. Costa
Cientista da Computação
Esp. Gestão em Telecom

EL MELECH NEEMAN!
אָמֵן

Reply via email to