diff --git a/matemat/webserver/util.py b/matemat/webserver/util.py index 74041d8..cd0d2ef 100644 --- a/matemat/webserver/util.py +++ b/matemat/webserver/util.py @@ -168,21 +168,27 @@ def parse_chf(value: str) -> int: :return: An integer representation of the value. :raises: Value error: If more than two digits after the decimal point are present. """ + # Remove optional leading "CHF" and strip whitespace value = value.strip() if value.startswith('CHF'): value = value[3:] value = value.strip() if '.' not in value: + # already is an integer; parse and turn into centimes return int(value, 10) * 100 + # Split at the decimal point full, frac = value.split('.', 1) if len(frac) > 2: raise ValueError('Needs max. 2 digits after decimal point') elif len(frac) < 2: + # Right-pad fraction with zeros ("x." -> "x.00", "x.x" -> "x.x0") frac = frac + '0' * (2 - len(frac)) + # Parse both parts ifrac: int = int(frac, 10) ifull: int = int(full, 10) if ifrac < 0: raise ValueError('Fraction part must not be negative.') if full.startswith('-'): ifrac = -ifrac + # Combine into centime integer return ifull * 100 + ifrac