Skip to navigation

Money

View as MarkdownOpen in Claude

An operations agent keeps amounts exactly, as Decimal, in the currency each Change gives or the service’s currency. Amounts in different currencies are never added together. After a second change that moves money, the model is told what the call comes to, per currency. Each settled change in the call’s record carries its money as an exact decimal string and its currency.

  • Change.money: one amount per write, as an int, float, numeric str, or Decimal. More than 0 is charged; less than 0 goes back to the caller.
  • Change.currency: the change’s ISO 4217 code, defaulting to the service’s.
  • currency= on Operations or ServiceAgent: the default code, checked to be three capital letters (ValueError otherwise).
  • money_format= on Operations or ServiceAgent: fn(amount, currency) replaces format_money for the totals the service states. amount is a Decimal, never below 0, and currency is the code or None.
from signalwire.operations import CURRENCIES, format_money

format_money

format_money(amount, currency=None) returns an amount as the model reads it, with the currency’s symbol or code and its own decimal places: $54.04, €54.04, ¥5,404, KWD 1.235. A code that isn’t in CURRENCIES goes after the amount (54.04 XOF). With no currency, the number stands alone, with cents only when it has any.

Parameters

amount
int | float | str | DecimalRequired

The amount. A bool, a non-numeric string, or a non-finite value raises an error.

currency
str | NoneDefaults to None

The ISO 4217 code. Must be three capital letters, or ValueError is raised.

Returns

str

CURRENCIES

A read-only mapping of 36 ISO 4217 codes to (prefix, decimal places), such as "USD": ("$", 2), "JPY": ("¥", 0), and "KWD": ("KWD ", 3). A symbol several currencies share is written so it isn’t ambiguous (CA$, A$).

Example

from signalwire.operations import ServiceAgent, format_money
agent = ServiceAgent("boutique", role="You answer the phone for a boutique in Lyon.", currency="EUR")
assert format_money(54.04, "EUR") == "€54.04"
assert format_money(5404, "JPY") == "¥5,404"
assert format_money("54.04", "XOF") == "54.04 XOF"