Money

The Money class is the core component of the library, representing a monetary amount with a specific currency.

class moneyx.Money(amount, currency='USD', rounding='HALF_UP')[source]

Bases: object

Represents a monetary amount in a specific currency.

The Money class handles all money-related operations with precision, ensuring correct decimal handling and currency validation.

Parameters:
  • amount (Decimal)

  • currency (Currency)

  • rounding (Literal['HALF_UP', 'HALF_DOWN', 'BANKERS', 'DOWN', 'UP', 'CEILING', 'FLOOR', 'HALF_ODD', 'HALF_TOWARDS_ZERO', 'HALF_AWAY_FROM_ZERO'])

__add__(other)[source]

Add two Money objects.

Parameters:

other (Money) – Money object to add

Return type:

Money

Returns:

A new Money object with the sum

Raises:

ValueError – If currencies don’t match

__sub__(other)[source]

Subtract Money object from this one.

Parameters:

other (Money) – Money object to subtract

Return type:

Money

Returns:

A new Money object with the difference

Raises:

ValueError – If currencies don’t match

__mul__(multiplier)[source]

Multiply Money by a number.

Parameters:

multiplier (Union[int, float, Decimal]) – Number to multiply by

Return type:

Money

Returns:

A new Money object with the product

__eq__(other)[source]

Compare two Money objects for equality.

Parameters:

other (object) – Another Money object to compare with

Return type:

bool

Returns:

True if both objects have the same amount and currency

__lt__(other)[source]

Compare if this Money object is less than another.

Parameters:

other (Money) – Another Money object to compare with

Return type:

bool

Returns:

True if this Money is less than other

Raises:

ValueError – If currencies don’t match

__gt__(other)[source]

Compare if this Money object is greater than another.

Parameters:

other (Money) – Another Money object to compare with

Return type:

bool

Returns:

True if this Money is greater than other

Raises:

ValueError – If currencies don’t match

__init__(amount, currency='USD', rounding='HALF_UP')[source]

Initialize a Money object.

Parameters:
  • amount (Union[Decimal, str, int, float]) – The monetary amount as a string, int, float, or Decimal

  • currency (str) – The ISO currency code (e.g., “USD”, “EUR”)

  • rounding (Literal['HALF_UP', 'HALF_DOWN', 'BANKERS', 'DOWN', 'UP', 'CEILING', 'FLOOR', 'HALF_ODD', 'HALF_TOWARDS_ZERO', 'HALF_AWAY_FROM_ZERO']) – The rounding mode to use for calculations

Raises:
Return type:

None

currency: Currency
rounding_mode: Literal['HALF_UP', 'HALF_DOWN', 'BANKERS', 'DOWN', 'UP', 'CEILING', 'FLOOR', 'HALF_ODD', 'HALF_TOWARDS_ZERO', 'HALF_AWAY_FROM_ZERO']
amount: Decimal
to_dict()[source]

Convert Money object to a dictionary.

Return type:

Dict[str, str]

Returns:

Dictionary with amount, currency, and rounding mode

format(locale='en-US')[source]

Format the money amount with currency symbol.

Parameters:

locale (str) – The locale to use for formatting

Return type:

str

Returns:

Formatted string representation

add(other)[source]

Add another Money object to this one.

Parameters:

other (Money) – Money object to add

Return type:

Money

Returns:

A new Money object with the sum

Raises:

ValueError – If currencies don’t match

subtract(other)[source]

Subtract another Money object from this one.

Parameters:

other (Money) – Money object to subtract

Return type:

Money

Returns:

A new Money object with the difference

Raises:

ValueError – If currencies don’t match

multiply(multiplier)[source]

Multiply this Money object by a number.

Parameters:

multiplier (Union[int, float, Decimal]) – Number to multiply by

Return type:

Money

Returns:

A new Money object with the product

allocate(ratios)[source]

Allocate this money amount according to a list of ratios.

Parameters:

ratios (List[Union[int, float, Decimal]]) – List of ratio values for allocation

Return type:

List[Money]

Returns:

List of Money objects with allocated amounts

Example

>>> Money("100.00", "USD").allocate([1, 1, 1])
[<Money 33.34 USD>, <Money 33.33 USD>, <Money 33.33 USD>]
with_tax(tax_rate_percent)[source]

Add tax to this money amount.

Parameters:

tax_rate_percent (float) – The tax rate as a percentage (e.g., 10 for 10%)

Return type:

Money

Returns:

A new Money object with tax included

extract_tax(tax_rate_percent)[source]

Calculate base amount and tax from a tax-inclusive amount.

Parameters:

tax_rate_percent (float) – The tax rate as a percentage (e.g., 10 for 10%)

Return type:

Dict[str, Money]

Returns:

Dictionary with “base” and “tax” components

convert_to(target_currency_code, rate)[source]

Convert this money amount to another currency.

Parameters:
  • target_currency_code (str) – ISO code of the target currency

  • rate (float) – Conversion rate from this currency to target

Return type:

Money

Returns:

A new Money object in the target currency

Example

>>> Money("100.00", "USD").convert_to("EUR", 0.85)
<Money 85.00 EUR>
split_evenly(n)[source]

Split this money amount evenly among n recipients.

Parameters:

n (int) – Number of recipients

Return type:

List[Money]

Returns:

List of Money objects with evenly split amounts

Raises:

ValueError – If n is less than 1

Example

>>> Money("100.00", "USD").split_evenly(3)
[<Money 33.34 USD>, <Money 33.33 USD>, <Money 33.33 USD>]
format_locale(locale='en_US')[source]

Format the money amount using locale-specific formatting.

Parameters:

locale (str) – The locale to use for formatting

Return type:

str

Returns:

Locale-formatted string representation

Example

>>> Money("1234.56", "USD").format_locale("de_DE")
'1.234,56 $'
to_json(indent=None)[source]

Convert Money object to a JSON string.

Parameters:

indent (Optional[int]) – Optional indentation for pretty-printing

Return type:

str

Returns:

JSON string representation

classmethod from_json(json_str)[source]

Create a Money object from a JSON string.

Parameters:

json_str (str) – JSON string representation

Return type:

TypeVar(T, bound= Money)

Returns:

New Money object

classmethod from_dict(data)[source]

Create a Money object from a dictionary.

Parameters:

data (Dict[str, Any]) – Dictionary with amount, currency, and optional rounding

Return type:

TypeVar(T, bound= Money)

Returns:

New Money object