Just like in the attached picture. I would like to calculate the value for the field "indeks_star" with the formula: indeks_star =[TOT_65__] / [TOT_0_14] * 100
And for features where in the field TOT_0_14 occurs 0 or in [TOT_65__] occurs 0, I would like the value -9999 to appear in the field indeks_star.
What formula to enter in the field calculator to simultaneously calculate all values for the field indeks_star?
Solved! Go to Solution.
I'm going to let you suggest the code then we can look at it.
def percentage(field1, field2) :
if field1 != 0 and field2 != 0 :
return ( (float(field1) / float(field2) ) * 100.0)
else :
return -9999
percentage( !TOT_65__!, !TOT!) ?
values seem correct, thanks for everything!
This type of basic calculation with a Null or zero check can be accomplished on one line using a Python conditional expressions, a.k.a., ternary operators,
-9999 if !TOT! == 0 else !TOT_65!/float(!TOT!)*100