Precise monetary calculations using integer cents with currency support and formatting
Project description
philiprehberger-money
Precise monetary calculations using integer cents with currency support and formatting.
Installation
pip install philiprehberger-money
Usage
Creating Money
from philiprehberger_money import Money
price = Money.from_major(19.99, "USD")
free = Money.zero("EUR")
Arithmetic
a = Money.from_major(10.00, "USD")
b = Money.from_major(3.50, "USD")
total = a + b # $13.50
diff = a - b # $6.50
doubled = a * 2 # $20.00
split = a.divide(3) # $3.33 (rounded)
negated = -a # -$10.00
Safe Currency Handling
usd = Money.from_major(10, "USD")
eur = Money.from_major(10, "EUR")
usd + eur # raises CurrencyMismatchError
Allocation (Split Without Losing Cents)
total = Money.from_major(100.00, "USD")
# Split 50/30/20
shares = total.allocate([50, 30, 20])
# [Money($50.00), Money($30.00), Money($20.00)]
# Handles remainders correctly
odd = Money.from_major(10.00, "USD")
thirds = odd.allocate([1, 1, 1])
# [Money($3.34), Money($3.33), Money($3.33)]
Denomination Rounding
price = Money.from_major(1.23, "USD")
# Round to nearest 5 cents
rounded = price.round_to_nearest(5)
print(rounded.format(symbol="$")) # "$1.25"
# Round to nearest 10 cents
rounded = price.round_to_nearest(10)
print(rounded.format(symbol="$")) # "$1.20"
Currency Conversion
usd = Money.from_major(100, "USD")
# Convert USD to EUR at rate 0.92
eur = usd.convert("EUR", 0.92)
print(eur.format()) # "92.00 EUR"
# Convert to zero-decimal currency
jpy = usd.convert("JPY", 149.5)
print(jpy.format(symbol="\u00a5")) # "\u00a514950"
Formatting
price = Money.from_major(1234.56, "USD")
print(price.format()) # "1234.56 USD"
print(price.format(symbol="$")) # "$1234.56"
Comparisons
a = Money.from_major(10, "USD")
b = Money.from_major(20, "USD")
a < b # True
a == b # False
a.is_positive() # True
a.is_zero() # False
Zero-Decimal Currencies
yen = Money.from_major(1000, "JPY")
print(yen.format(symbol="\u00a5")) # "\u00a51000"
Serialization
m = Money.from_major(19.99, "USD")
d = m.to_dict() # {"amount_cents": 1999, "currency": "USD"}
m2 = Money.from_dict(d) # Money(19.99 USD)
API
| Function / Class | Description |
|---|---|
Money.from_major(amount, currency) |
Create from major units (dollars, euros, etc.) |
Money.zero(currency) |
Create zero-value Money |
Money.from_dict(data) |
Create from dict |
.add(other) / + |
Add two Money values (same currency) |
.subtract(other) / - |
Subtract (same currency) |
.multiply(factor) / * |
Multiply by number |
.divide(divisor) |
Divide by number |
.allocate(ratios) |
Split into parts without losing cents |
.round_to_nearest(step) |
Round minor units to nearest multiple |
.convert(target_currency, rate) |
Convert to another currency at given rate |
.negate() / -m |
Negate amount |
.abs() |
Absolute value |
.is_zero() / .is_positive() / .is_negative() |
Predicates |
.format(symbol=None) |
Format as string |
.to_dict() |
Serialize to dict |
.amount |
Major unit value as float |
.decimals |
Currency decimal places |
CurrencyMismatchError |
Raised on mixed-currency operations |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this package useful, consider giving it a star on GitHub — it helps motivate continued maintenance and development.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_money-0.3.0.tar.gz.
File metadata
- Download URL: philiprehberger_money-0.3.0.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c5e6990eee8f3fb3220f5809d06caee4375fdee5f283c4be8cfc79f5cdf2c0ae
|
|
| MD5 |
7e04af10c7ac1ec59cf23b2c46461056
|
|
| BLAKE2b-256 |
02811989acaea53f3ae05d87ad76a77bbdc735011907bbdf77f2c36b02fc6dda
|
File details
Details for the file philiprehberger_money-0.3.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_money-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8498c0ac20e4d7b7974fb112b51deb2366ccf49b9aff26789a8c89dbe2352151
|
|
| MD5 |
f0696e6c165eba880f6d513a5c087dc9
|
|
| BLAKE2b-256 |
b0bace9b5dbad1f4b19ab7e34e0bb31128781efec5bda27d051b0becc7e788ca
|