Find capacity risks before rounding
Calculate a precise free-space percentage and keep unavailable data visible.
A percentage is a calculation, not a label
Compute free space as free / total * 100. Classify the unrounded result before formatting it for a report. A value just below a threshold might display as the threshold when rounded, yet still require attention under the documented policy.
This rule marks a system low only when its percentage is strictly less than the minimum. Equality is acceptable. That boundary is a policy choice; making it explicit lets another administrator reproduce your decision.
Unknown must stay different from healthy
An export may omit a measurement or contain an impossible value. Zero total space cannot be a valid denominator, negative free space is invalid, and free space cannot exceed the total. Return status unknown and a None percentage for these observations. Treating them as zero percent would imply a real full disk; treating them as healthy would hide a collection problem.
Validate the threshold separately. Invalid policy is a tool configuration error and raises ValueError, even when a measurement is unknown. Reject booleans and non-finite values such as infinity or NaN rather than allowing Python's numeric coercions to produce an accidental decision.
The same function can work with bytes, GiB, or another consistent unit because it uses a ratio. Both measurements must use the same unit. Our browser lab uses supplied numbers and makes no filesystem-capacity query against your device.
total, free = 100_000, 19_999
percent = free / total * 100
print(percent, round(percent, 1), percent < 20)
Put it into practice.
Define capacity_status(total, free, minimum). A finite numeric minimum from 0 through 100 is required, excluding booleans; invalid policy raises ValueError. Invalid measurements (non-numeric/non-finite/bool, total <= 0, free < 0 or free > total) return {status: "unknown", free_percent: None}. Otherwise return the unrounded percentage and status low when strictly below minimum, ok otherwise. Do not round before classifying.
See an explained solution
import math
def capacity_status(total, free, minimum):
numeric = lambda value: type(value) in (int, float) and math.isfinite(value)
if not numeric(minimum) or not 0 <= minimum <= 100:
raise ValueError("Minimum must be a finite percentage")
if not numeric(total) or not numeric(free) or total <= 0 or free < 0 or free > total:
return {"status": "unknown", "free_percent": None}
percent = free / total * 100
return {"status": "low" if percent < minimum else "ok", "free_percent": percent}
if __name__ == "__main__":
print(capacity_status(100_000, 19_999, 20))
Validation separates configuration mistakes from unknown observations. The strict unrounded comparison correctly distinguishes the threshold from values just below it.