impl/python/to_basis_points.py
781 bytes · the Python implementation · view raw
Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
from .math_basis_points_convert_rate import convert_rate ← convertRate, another function of this group · built into the same file, even by a slim install
from .math_basis_points_types import RateUnit
_MAX_SAFE = 9007199254740991
def to_basis_points(value: str, unit: RateUnit) -> int:
"""A rate as whole basis points, the unit the registry keeps rates in.
Refuses a rate that is not a whole number of basis points (12.345% is
1234.5) rather than round it: which way to round a rate is the caller's
policy, not a conversion's.
"""
text = convert_rate(value, unit, "basis-points")
if "." in text:
raise ValueError("%s %s is not a whole number of basis points" % (value, unit))
bp = int(text)
if bp > _MAX_SAFE or bp < -_MAX_SAFE:
raise ValueError("%s %s is too large for integer basis points" % (value, unit))
return bp