Ratios, Percentages, and Fractional Rates

Represent a percentage with an explicit scale so a calculation and its display cannot accidentally multiply or divide by one hundred twice.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Use one internal convention
  2. Check a second example
  3. Put a number on both sides of the scale change
  4. Use a fixture that reveals overconversion

Use one internal convention

A rate of 12 percent can be represented as \(0.12\). Multiplying a price of 50 by that fraction gives a discount of 6. The formatter then displays the rate as 12 percent by applying the display scale. See parsing and formatting for where that translation belongs.

Check a second example

A setting of 0.5 percent corresponds to the multiplier 0.005. A test with ten percent alone can miss rounding of small rates. Include small and exact cases and choose units explicitly in names and interfaces.[1][2]

Put a number on both sides of the scale change

A five-percent rate corresponds to multiplier 0.05. On a nonzero amount of 80 this gives four. A form field labelled percent might accept a supplied value of five and perform one conversion at its boundary. A configuration accepting fractional multipliers might receive 0.05 directly. Feeding five into the second interface would yield 400. Use names expressing which representation is stored. See parsing and formatting.

Use a fixture that reveals overconversion

With zero amount, every rate computation could still return zero. With 80 and half a percent, the expected result is 0.4. This tests that the rate becomes 0.005 and is not rounded prematurely. Verify the accepted input representation and its displayed conversion separately.

Suppose a report compares outcomes with counts 3 of 10 and 5 of 20. Both ratios and denominators are needed when describing what changed. A pooled fraction is 8 of 30. The average of the percentages answers a different weighting question. See combining rates across groups. The interface can label the intended output explicitly so readers do not confuse selection rate and percentage-point change.

References

  1. Python Software Foundation, Python Standard Library: Built-in Types ↩
  2. Python Software Foundation, Python Language Reference: Expressions ↩