I am trying to calculate a percentage in a field in a table. customers and total_customers are short integer fields.
arcpy.AddField_management(tableSepSel2, "percent", "DOUBLE")
arcpy.CalculateField_management(tableSepSel2, "percent", "!customers! / !total_customers!*100", "PYTHON_9.3")
I don't get any errors but all the values in the percent field are zero when most of them should not be zero.
If I go into ArcMap and use the field calculator with the Python parser, the same thing happens. However, when I choose the VBScript parser in ArcMap and redo the code using brackets it works fine.
1 Answer 1
I'm guessing customers
and total_customers
are integer fields, and the equation is using integer math, yielding the rounded-down result (0 * 100
) in every case except where customers == total_customers
(1 * 100
).
Use this equation to get decimal calculations:
"float(!customers!) / float(!total_customers!) * 100"
-
Aha! Yes they are integer fields. That worked. Thanks!Andrew– Andrew2014年09月22日 16:15:08 +00:00Commented Sep 22, 2014 at 16:15
Explore related questions
See similar questions with these tags.