examples: to_gbp currency normalisation helper
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Viktor Barzin 2026-05-28 22:24:47 +00:00
parent e75a635d25
commit 0d442de918
2 changed files with 37 additions and 0 deletions

View file

@ -179,3 +179,22 @@ async def extract_with_fallback(
client=client,
)
return secondary or primary
def to_gbp(
amount: Decimal | None,
currency: str | None,
rates: dict[str, Decimal],
) -> Decimal | None:
"""Convert `amount` in `currency` to GBP using `fx.fetch_rates` output.
`rates[X]` = "how much GBP one unit of X is worth" the convention
used by `fire_planner/fx.py`. Returns None when amount/currency is
missing or the currency isn't in `rates`.
"""
if amount is None or currency is None:
return None
rate = rates.get(currency.upper())
if rate is None:
return None
return (amount * rate).quantize(Decimal("0.01"))