[web2py] Re: Decimal vs float

2011-12-02 Thread Cliff
No need. The Python tutorial says mixing integers and decimals in arithmetic is OK. Note version 2.7.1 in the following. cjk@mid-tower:~$ python Python 2.7.1+ (r271:86832, Apr 11 2011, 18:13:53) [GCC 4.5.2] on linux2 Type help, copyright, credits or license for more information. from decimal

[web2py] Re: Decimal vs float

2011-12-01 Thread pbreit
Examples. shipping_amount and start_price both decimals. if form.vars.shipping_amount (form.vars.start_price * 0.2): TypeError: unsupported operand type(s) for *: 'Decimal' and 'float' drops is integer, price_change, start_price are decimals. if ((form.vars.drops * form.vars.price_change)

[web2py] Re: Decimal vs float

2011-12-01 Thread pbreit
Also. I think this is a Python thing, not Web2py. I'm at a loss for what data type I should use for currencies. I wasn't expecting to run into these type problems (I'm a relative newbie!). if form.vars.shipping_amount (form.vars.start_price * Decimal(0.2)): File

[web2py] Re: Decimal vs float

2011-12-01 Thread pbreit
A version gotcha. This is OK on 2.7 but errors on 2.6: from decimal import * Decimal(1.0) Decimal('1') from decimal import * Decimal(1.0) Traceback (most recent call last): File stdin, line 1, in module File /usr/lib/python2.6/decimal.py, line 649, in __new__ First convert the float

[web2py] Re: Decimal vs float

2011-12-01 Thread Ross Peoples
Is 'decimal' the best field type for storing financial amounts that will be involved in math calculations? I've been doing a lot of research on the decimal vs float thing for a while, and every database admin I've ever talked to about it says the same thing: When it comes to financial

[web2py] Re: Decimal vs float

2011-12-01 Thread Ross Peoples
Further examples: quantity = 20.5 unit_cost = Decimal('2.99') total_cost = Decimal(str(quantity)) * unit_cost Or using the the example you provided above: if ((form.vars.drops * form.vars.price_change) / form.vars.start_price) 0.20 Assuming that form.vars.drops is a float, and

[web2py] Re: Decimal vs float

2011-12-01 Thread Cliff
if ((form.vars.drops * form.vars.price_change) / form.vars.start_price) 0.20: TypeError: can't multiply sequence by non-int of type 'Decimal' I think form.vars.drops is a string. I usually do something like Decimal(form.vars.somedecimalfield or 0) For the benefit of Python newcomers reading

[web2py] Re: Decimal vs float

2011-12-01 Thread Cliff
I forgot to say the Python tutorial says to use Decimal for financial calcs to avoid rounding errors. On Nov 27, 8:33 pm, pbreit pbreitenb...@gmail.com wrote: I have some price fields specified as type 'decimal'. But am finding that I have to cast to 'float' do do any math (or the other to

[web2py] Re: Decimal vs float

2011-12-01 Thread pbreit
Great info, thanks. I hadn't thought of using Decimal type for integers that might be involved in calculations. Can I simulate an integer with something like decimal(4,0)? For quantity, I'd rather show 1 and not 1.0.

[web2py] Re: Decimal vs float

2011-11-27 Thread Massimo Di Pierro
You should not need any casting to do math computations. Can you provide an example of what you do, perhaps something does not behave as intended. On Nov 27, 7:33 pm, pbreit pbreitenb...@gmail.com wrote: I have some price fields specified as type 'decimal'. But am finding that I have to cast to

[web2py] Re: Decimal vs Float and Documentation

2010-06-01 Thread mdipierro
There is no need to use SQLCustomType in this case. Decimal is supported by web2py type='decimal(n,m)' if the underliying database supports it. The problem is representation of the number. Try this: class IS_MYDECIMAL(IS_DECIMAL_IN_RANGE): def formatter(self,value): return '%.2f' % value

[web2py] Re: Decimal vs Float and Documentation

2010-06-01 Thread NetAdmin
In which file do I declare the class? Thanks to everyone for their answers. On Jun 1, 3:10 pm, mdipierro mdipie...@cs.depaul.edu wrote: There is no need to use SQLCustomType in this case. Decimal is supported by web2py type='decimal(n,m)' if the underliying database supports it. The

[web2py] Re: Decimal vs Float and Documentation

2010-06-01 Thread mdipierro
Before the model in the model file will do. On Jun 1, 3:39 pm, NetAdmin mr.netad...@gmail.com wrote: In which file do I declare the class? Thanks to everyone for their answers. On Jun 1, 3:10 pm, mdipierro mdipie...@cs.depaul.edu wrote: There is no need to use SQLCustomType in this case.