2 years ago

#36690

test-img

Edoardo Zucchelli

Django annotate division does't calculate float numbers correctly

I'm trying to divide numbers that are stored in database in millionth, but the decimals are lost.

conditions_set.annotate(
                    _amount_from=ExpressionWrapper(F('amount_from')/1000000, output_field=FloatField()),
                    _amount_to=ExpressionWrapper(F('amount_to')/1000000, output_field=FloatField()),
                    _value=ExpressionWrapper(F('value')/10000, output_field=FloatField())
).values('_amount_from', '_amount_to', '_value').order_by('amount_from')
0 = {dict: 3} {'_amount_from': 0.0, '_amount_to': 1000.0, '_value': 0.0}
1 = {dict: 3} {'_amount_from': 1000.0, '_amount_to': 3000.0, '_value': 0.0}

Amount from of key 1 is equal to 1000001, so I'm expecting that the result of 1000010000 / 1000000 is 1.000,01. As the 'value' is equal to 6800, I'm expecting that the result of 6800/10000 is 0,68. But I get 0.0.

python

django

division

annotate

0 Answers

Your Answer

Accepted video resources