rounding
Turning a number with a fractional part into a whole number by a chosen rule, such as nearest, towards zero or half to even.
In more detail
Python's int() truncates towards zero, which quietly drops fractions of a penny, while round() goes to the nearest whole number. When a value sits exactly halfway, round() picks the even neighbour, so round(2.5) is 2. For money that is a policy decision, and tests with awkward cases are how you pin it down.
Related terms
- floating-point number — A number with a fractional part stored in binary, which can only approximate most decimal values.
- decimal module — Python's standard library module for exact decimal arithmetic, used where binary floats would round wrongly, such as money.
- edge case — An input at the limits of what code must handle, such as an empty list, zero, a single item or the largest allowed value.
Taught in
- One Assertion Per Reason — Automated Testing, TDD & Quality Engineering
- Parametrised Tests — Automated Testing, TDD & Quality Engineering