Skip to main content

Like a quantum object that can be in many states before observation, quantum-money only performs its calculations when someone tries to observe the value.

Project description

quantum-money

Eager and lazy evaluation library for monetary calculations in Python.

quantum-money provides two classes: Money for straightforward calculations with high internal precision, and QMoney for lazy evaluation where you need full control over rounding order. QMoney builds an expression tree and only evaluates when you call .observe(), which returns a Money object.

Why?

In financial software, rounding order changes results:

from decimal import Decimal, ROUND_HALF_UP
from quantum_money import QMoney

unit_price = QMoney(Decimal("1.345"))

# Round first, then multiply → 13.50
round_first = (unit_price.round(ROUND_HALF_UP) * 10).observe().raw_amount

# Multiply first, then round → 13.45
round_last = (unit_price * 10).round(ROUND_HALF_UP).observe().raw_amount

assert round_first != round_last  # Rounding order matters!

With quantum-money, you decide exactly where rounding happens.

Installation

pip install quantum-money

Or with uv:

uv add quantum-money

Requires Python 3.12+.

Quick start

Money — eager calculations

from quantum_money import Money

price = Money("29.99")
tax = Money("2.40")

total = price + tax
print(total.real_amount)  # 32.39
print(total.cents)        # 3239

Money accepts Decimal, str, int, or float. It maintains full internal precision (raw_amount) and provides a rounded 2-decimal-place view (real_amount).

QMoney — lazy calculations with rounding control

from decimal import Decimal
from quantum_money import QMoney

# Create monetary values (must use Decimal)
price = QMoney(Decimal("29.99"))
tax = QMoney(Decimal("2.40"))

# Build an expression tree — nothing is calculated yet
total = price + tax
print(repr(total))  # QMoney((29.99 + 2.40))

# Evaluate when you're ready — returns a Money object
result = total.observe()
print(result.raw_amount)  # 32.39

The bridge: QMoney → Money

.observe() evaluates the expression tree and returns a Money object:

from decimal import Decimal, ROUND_HALF_UP
from quantum_money import Money, QMoney

# Build a lazy expression
expr = (QMoney(Decimal("10.33")) * 3).round(ROUND_HALF_UP)

# Observe collapses QMoney → Money
result = expr.observe()
assert isinstance(result, Money)

print(result.raw_amount)   # Decimal('30.99')
print(result.real_amount)  # Decimal('30.99')
print(result.cents)        # 3099

Supported operations

a = QMoney(Decimal("100"))
b = QMoney(Decimal("5.50"))

a + b           # Addition
a - b           # Subtraction
a * 3           # Multiply by scalar (int or Decimal)
a / 4           # Divide by scalar
a ** 2          # Power
a.root(2)       # Nth root
-a              # Negation
a.round()       # Round (default: ROUND_HALF_UP, 2 places)
sum([a, b])     # Works with sum()

Documentation

Full documentation: quantum-money docs

Development

git clone https://github.com/your-username/quantum-money.git
cd quantum-money
uv sync --dev
uv run pytest              # Run tests
uv run ruff check .        # Lint
uv run black .             # Format
uv run mkdocs serve        # Serve docs locally

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

quantum_money-0.1.0.tar.gz (79.5 kB view details)

Uploaded Source

Built Distribution

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

quantum_money-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file quantum_money-0.1.0.tar.gz.

File metadata

  • Download URL: quantum_money-0.1.0.tar.gz
  • Upload date:
  • Size: 79.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for quantum_money-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6437e5ea7d9b1464faf47849646aaa809cef2e30c4e83ff3ff2220118b08c0a7
MD5 6a76fe1cabbfb975506edca702bc644a
BLAKE2b-256 f532ff2296ab2f9da2d207c65db8a0343462912c351b7686d71e52ffc417acc6

See more details on using hashes here.

Provenance

The following attestation bundles were made for quantum_money-0.1.0.tar.gz:

Publisher: ci.yml on tomascorrea/quantum-money

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file quantum_money-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: quantum_money-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for quantum_money-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 82a42dfb021bcdc02184a92cf9848656ac778755915a057d3754490b48cb9b0a
MD5 3b761c140d651f1c9eaec2474880c331
BLAKE2b-256 a458a7107b27c9bd7418be7308bdc759061ac14eb576d9f2f6750ca14fc8d02a

See more details on using hashes here.

Provenance

The following attestation bundles were made for quantum_money-0.1.0-py3-none-any.whl:

Publisher: ci.yml on tomascorrea/quantum-money

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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