py-money is a Python 3 library that provides `Money` and `Currency` classes for precise monetary calculations. It enforces correct decimal places for currencies, leverages Python's `Decimal` type to prevent floating-point errors, and supports basic arithmetic and logical operations for immutable money objects. The current version is 0.5.0, with releases historically focused on bug fixes and minor feature enhancements.
pip install py-moneyVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create `Money` objects, perform basic arithmetic operations, and work with currency subunits. It highlights the importance of using the correct import paths and shows how to access the amount and currency code.
Manually convert amounts to a common currency before performing operations, or ensure all operations are between `Money` objects of the same currency.
Initialize `Money` objects with `Money('100.50', USD)` or `Money(Decimal('100.50'), USD)` instead of `Money(100.50, USD)`.Ensure monetary amounts conform to the standard decimal places for their respective currencies (e.g., 2 for USD, 0 for JPY) before creating `Money` objects.
Be mindful of the order of operations and simplify expressions where intermediate rounding might be problematic. For critical financial calculations, evaluate if this rounding behavior meets specific accounting requirements.
Ensure your `babel` dependency, if used for formatting, is within the compatible range, or upgrade `py-money` to `0.4.0` or newer.
Upgrade to `py-money` v0.3.0 or later to utilize the `Money.from_sub_units()` method and the `.sub_units` property.
Ensure both `Money` objects in an operation have the same `Currency`. If cross-currency operations are required, amounts must be manually converted to a common currency before performing the operation, as direct conversion is not supported by `py-money`.
Ensure the monetary amount string or `Decimal` conforms to the standard decimal places for its respective currency (e.g., '123.45' for USD, '100' for JPY) before creating `Money` objects.
Use explicit imports for the classes: `from money.money import Money` and `from money.currency import Currency`.
Always initialize `Money` objects with the amount as a string (e.g., `Money('10.50', Currency.USD)`) or a `Decimal` object (e.g., `Money(Decimal('10.50'), Currency.USD)`), rather than a `float`.