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
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)
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
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
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
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
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
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
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.
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
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
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
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.
13 matches
Mail list logo