Skip to main content

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)

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="¥"))  # "¥1000"

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.1.1.tar.gz (4.0 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.1.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for philiprehberger_money-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c3b037860ccae497f0ce0f563d12df52bae7bdd42387a79ceaeecc7b04fb5b0b
MD5 d60ab69342b4f58832832814159a119a
BLAKE2b-256 c75badaf6727bdea338a3c4ec3c8baa259a49bd3e5ec14bf51a5c6805ad0fc6e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_money-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d0e4f7a72cf3d13700375ac381f59c2c52b82fe8d80c1fbe06c0762514f71200
MD5 88bdb85f37f44ae3fd15517a2fbbb4e1
BLAKE2b-256 0f9ed0ae9560b26edd138a1ed79aa3785feddc065937fd31646e283cfd4f74ef

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