# FIeld calculator gives integer not a real as requested

Two fields "population" and "F06_popula" (both integers) were used in calculating a new field pct-diff. I asked that the new field be a decimal with a width of 10 and precision of 3. The result has no decimals. See the graphic. But the Layer Properties indicates that pct-diff is a real with 3 decimals. Why are the decimals not visible? Thanks -

• I can't check right now, but have you tried multiplying the field values by 1.0? Sometimes you need to cast the integers to reals lik that... – Jose Aug 14 '12 at 22:23
• Yes that works. It seems that it would be better to not do integer calculations except unless mod() functions are used. The default should be floating point and not dependent on the variable type. – HealthMaps Aug 15 '12 at 17:23

## 2 Answers

I read a discussion http://hub.qgis.org/issues/5153 on the problem which no one seemed to agree on but a solution was found and far from obvious to me.

In the field calculator under conversions in the functions list, use toreal like this: toreal(field1)/field2

it's more like unreal huh?

• toreal( "F06_POPULA" - "POPULATION") / "POPULATION" works. – HealthMaps Aug 14 '12 at 22:17
• Thank you! and ("F06_POPULA" - "POPULATION") /toreal( "POPULATION") Got to have a floating point in there somewhere. Also multiplying by 1. will not work, but 1.0 does work. – HealthMaps Aug 14 '12 at 22:26

The formula ("F06-popula"-"population")/"population" is made up of all integer variables. Apparently the field calculator therefore did integer divide. When I multiplied the numerator by 1.0 ( one with a decimal) the result appeared as a floating point number. This was a surprise to me because the statistical languages I use (SAS,R,STATA) always does floating point arithmetic. So do the commercial GIS I use, TransCad.