22 lines
536 B
Python
22 lines
536 B
Python
|
|
from decimal import ROUND_HALF_UP, Decimal
|
||
|
|
|
||
|
|
HUNDRED = Decimal("100")
|
||
|
|
ZERO = Decimal("0")
|
||
|
|
TWO_PLACES = Decimal("0.01")
|
||
|
|
|
||
|
|
|
||
|
|
def clamp(value: Decimal) -> Decimal:
|
||
|
|
return min(HUNDRED, max(ZERO, value))
|
||
|
|
|
||
|
|
|
||
|
|
def round_score(value: Decimal) -> Decimal:
|
||
|
|
return value.quantize(TWO_PLACES, rounding=ROUND_HALF_UP)
|
||
|
|
|
||
|
|
|
||
|
|
def as_decimal(value: object) -> Decimal:
|
||
|
|
if isinstance(value, Decimal):
|
||
|
|
return value
|
||
|
|
if isinstance(value, bool) or value is None:
|
||
|
|
raise TypeError("value is not numeric")
|
||
|
|
return Decimal(str(value))
|