PyMoneyed

3.0 · active · verified Sun Apr 12

PyMoneyed provides robust Currency and Money classes for handling monetary values in Python. It is currently at version 3.0 and has a release cadence tied to significant feature additions and Python version support changes, ensuring compatibility and modern practices.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create `Money` objects, perform basic arithmetic, and access their components. It highlights the importance of using `Decimal` or string for amounts to maintain precision and shows how `Money` enforces currency-aware operations.

from moneyed import Money, USD, EUR
from decimal import Decimal

# Instantiate Money with Decimal or string for precision
price_usd = Money(amount='99.99', currency=USD)
price_eur = Money(Decimal('75.50'), EUR)

print(f"USD Price: {price_usd}")
print(f"EUR Price: {price_eur}")

# Arithmetic operations (currency-aware)
total_price = price_usd + Money('10.01', USD)
print(f"Total USD: {total_price}")

# Invalid operations raise TypeError
try:
    invalid_sum = price_usd + price_eur
except TypeError as e:
    print(f"Error: {e}")

# Access components
print(f"Amount: {total_price.amount}, Currency Code: {total_price.currency.code}")

# Get amount in sub-units (e.g., cents for USD)
print(f"USD Price in cents: {price_usd.get_amount_in_sub_unit()}")

view raw JSON →