Boris The Animal Boris The Animal - 3 months ago 8x
Python Question

Is it possible that if I multiply 2 floats the result will be different then multiplication of 2 decimals?

I am calculating a field in my view file

data_dict = { 'quantity_order': float(bom.quantity) * float(quantity)}

I added float conversion since I was getting a different problem:

"can't multiply sequence by non-int of type 'Decimal'"

In model both fields are defined as decimal

quantity_order = models.DecimalField(max_digits=19, decimal_places=3)

one of the fields is received via url regular expression

url(r'^production/order/new/(?P<pk_bom>\d+)/(?P<pk_soproduct>\d+)/(?P<uri>\S+)/(?P<quantity>\d+(\.\d{1,3}))/$',views.Production_order_new, name="production_order_new"),

So the issue is that sometimes the calculation is not correct.
For example if both quantity values are
then it gets calculated correctly, but when one value is
44 * 666
the result will be

Can it be the converting values to float before multiplication is causing this error?


"can't multiply sequence by non-int of type 'Decimal'"

What this actually means is that one of your operands is a sequence and the other is a Decimal. Ie, one of bom.quantity or quantity is a sequence and not an int, float or decimal.

data_dict = { 'quantity_order': float(bom.quantity) * float(quantity)}

A sequence can be multplied by an int.

seq = [1,2,3]
seq * 2 # produces  [1,2,3,1,2,3]


seq * 2.5 # produces can't multiply sequence by non-int of type 'float'