Solution 1)

from decimal import Decimal
t.fee = Decimal('0.3')
result = t.bal - t.fee # result is in Decimal

or 

t.fee =0.3
result = float(t.bal) - t.fee # result is in float

On Wednesday, 9 September 2015 23:25:48 UTC-5, Dmitri Ermolaev wrote:
>
> in db:
>
>    Field('bal', 'decimal(16,8)', default = Decimal(0)),
>    Field('fee', 'decimal(5,3)', default = Decimal(0)),
>
>
> in appadmin I set values
>
> in controller:
>
> t.fee =0.3
>
> t.bal - t.fee -> error! float and decimal operation
>
> controller:
>
> # -*- coding: utf-8 -*-
>
> ##session.forget()
> ##cache_expire = not request.is_local and 300 or 0
>
> ##@cache.action(time_expire=300, cache_model=cache.ram, quick='P')
> def index():
>     
>     rec = None
>     if request.args(0) == 'tru':
>         db.d.truncate()
>         rec_id = db.d.insert( bal= Decimal(123.3456789), fee = 2.3)
>     elif request.args(0) == 'ins':
>         rec_id = db.d.insert( bal= Decimal(request.args(1)), fee = 
> float(request.args(2)))
>     elif request.args(0) == 'help':
>         return CAT(
>             H3('/tru - truncate'),
>             H3('/ins/[BAL]/[FEE] - insert'),
>             )
>     else:
>         rec = db(db.d).select().last()
>     
>     rec = rec or db.d[ rec_id ]
>     rec.bal = 23.44  ## bal ---> is float!!!
>     print 'rec.bal = 23.44 type:', type(rec.bal)
>     rec.update_record()  ## bal ---> is float!!!
>     
>     ### ERROR 1:
>     try:
>         rec.update_record( bal = rec.bal - Decimal(2.4), fee=2.1 ) ## 
>  ERROR!!!
>     except:
>         print "<type 'exceptions.TypeError'> unsupported operand type(s) 
> for -: 'float' and 'Decimal'"
>     
>     rec.update_record( fee=2.1 ) ##  fee now is float!
>     
>     # reload vars
>     rec_id = rec.id
>     rec = db.d[ rec_id ] ## now all vars is Decimal
>
>     ### ERROR 2 - in admin db edit:
>     '''
>     If I edit record with bal=0 in app/appadmin/update/db/d/3
>     Bal:
>     0E-8.00  <-- I simple click Submit and it raise error:
>         Enter a number between -1e+10 and 1e+10
>     '''
>     
>     return dict(h=rec)
>
>
>
>
>
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to