Documentation

Estimating Totals with Multiplication

Sometimes a useful number is not asked directly but can be estimated by combining two answers. When one of those answers is a percentage, the pattern is multiply-then-rescale. In the example healthcare survey, respondents give their household size and the percentage of the household with a chronic condition; from those we estimate the number of affected members.

The recipe
from survey import Recoder

r = Recoder(**globals())

household_size = r.get_value("household-size") or 0
percent_with_condition = r.get_value("percent-household-chronic-condition") or 0

r.store_value("Estimated chronic-condition members", household_size * percent_with_condition // 100)
How it works

Both inputs are read with get_value and guarded with or 0. The estimate multiplies the household size by the percentage and then divides by 100, because the percentage is captured as a number from 0 to 100 rather than a fraction. So a household of 4 with 50 percent affected yields 4 * 50 // 100, which is 2.

The // 100 floor division keeps the result a whole number — you cannot have a fraction of a person. Order of operations matters here: Python evaluates household_size * percent_with_condition first, then the floor division, so the rounding happens once at the end rather than midway through.

Variations

If your percentage is already stored as a fraction (0 to 1), drop the / 100 and multiply directly:

r.store_value("Estimated chronic-condition members", round(household_size * fraction_with_condition))

When you do not want to round at all — for instance, when the estimate feeds a further calculation — use * and / and keep the floating-point result, rounding only at the final reporting step.