Skip to main content

Precise monetary calculations using integer cents with currency support and formatting

Project description

philiprehberger-money

Tests PyPI version License

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)]

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

Method 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
.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

Development

pip install -e .
python -m pytest tests/ -v

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_money-0.2.5.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_money-0.2.5-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_money-0.2.5.tar.gz.

File metadata

  • Download URL: philiprehberger_money-0.2.5.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_money-0.2.5.tar.gz
Algorithm Hash digest
SHA256 79e14743a8429262daa84d85e72ae888b6d08c9ddc94b0a541892d51992be7a7
MD5 cb48434ee451876304c1ea1b149a4905
BLAKE2b-256 36bc64a072983fb176fd14e1f92ca048b3e8e47a512176ab3e6c417e73beec87

See more details on using hashes here.

File details

Details for the file philiprehberger_money-0.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_money-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 78d67afad831ecc5bf60834a9a86bb829bff6ec6afc82a733834d7facf5b2e13
MD5 7d06c4c0e9e914f57abea12b7c3a63e4
BLAKE2b-256 1d5376dc663467e8a89ddb8f8fc274a7adb3e1e2c1cbbcf2b4d88587c17e0a5d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page