# basic problem using division in field calculator

This is a very basic question, but I can't figure out what's going on. I've got election results I'd like to simply display as percentages rather than vote totals, which should be as simple as dividing the column "ElectionYes" by "ElectionTotal". When I write that expression ("ElectionYes" / "ElectionTotal") into the field calculator or layer display box, though, it spits out "0" for all the rows! What gives? I should be seeing returns ranging from .402 to .670.

I've made sure that all the columns are integers, all the data is saved, and there are no null values in either column. All the other mathematical functions work (as in, I can create a column of "ElectionYes" * "ElectionTotal" with no errors), but I can't figure out how to do it with division. Any ideas?

• Did you set decimal places to your integer field? Try ('ElectionYes' / 'ElectionTotal') * 100 – MAP Dec 12 '14 at 7:03

You need to cast your denominator to a float (decimal) value. See this thread: Why is this field calculator expression evaluated incorrectly when using Python syntax?

Your output field also must accept decimal values (i.e. cannot be an integer field).

By default QGIS expressions do int / int = int which is why you see the result you do. So you need to do `float(ElectionYes) / ElectionTotal` which will do `float/int=float`

However in QGIS 2.8 this has now change to do `int/int = float` so it will do what you are expecting.

Another way to convert to real numbers would be (toreal("ElectionYes") / "ElectionTotal"

• Did you try the expression? – Germán Carrillo Feb 28 '15 at 20:18
• Short of actually testing it, at the very least it looks like you're missing a parenthesis somewhere in your expression even if the toreal function would work. – Chris W Feb 28 '15 at 21:10
• Yup, missing the closing parenthesis, so the correct solution is (toreal("ElectionYes") / "ElectionTotal") – Jan K Mar 21 '15 at 20:20
• also, you need an underscore: to_real() – Ben Jul 19 at 14:25