Functional Weave
Code in TypeScript

banking.cheque-amount-words@1.0.0

impl/python.py

1,429 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 .banking_cheque_amount_words_types import ChequePenceStyle, ChequeWording
from .money_amount import Money, money  ← from money.amount ^1.0.0 · built alongside by fune
from .money_format import format_money  ← from money.format ^1.0.0 · built alongside by fune
from .text_number_to_words import amount_in_words  ← from text.number-to-words ^1.0.0 · built alongside by fune

#: The abbreviation written after pence as figures: 50p, 50c.
_MINOR_SUFFIX = {"GBP": "p", "EUR": "c", "USD": "c"}


def cheque_amount_words(amount: Money, pence_style: ChequePenceStyle) -> ChequeWording:
    """The amount lines of a cheque.

    The words come from text.number-to-words, capitalised and closed with
    "only" so nothing can be added after them; the figures come from
    money.format.
    """
    if amount.minor <= 0:
        raise ValueError("a cheque amount must be greater than zero, received %d" % (amount.minor,))
    if pence_style == "words":
        body = amount_in_words(amount)
    elif pence_style == "figures":
        # The pounds are always in words, even when there are none, so the
        # line never starts with a figure that could be altered.
        major, minor = divmod(amount.minor, 100)
        body = amount_in_words(money(major * 100, amount.currency))
        if minor > 0:
            body += " and %d%s" % (minor, _MINOR_SUFFIX[amount.currency])
    else:
        raise ValueError('unknown pence style "%s": expected words or figures' % (pence_style,))
    words = body[0].upper() + body[1:] + " only"
    return ChequeWording(words=words, figures=format_money(amount))