Skip to main content

larzmoney

Exact, penny-perfect money for Python. Zero dependencies.

A Money is an integer count of minor units plus a currency, and it never becomes a float. $10.99 is stored as the integer 1099, not the float 10.99 (which doesn't exist exactly in binary and is the root of every "off by a cent" bug). All arithmetic is integer arithmetic; rounding only happens where you multiply or divide, and there you choose the mode.

from larzmoney import Money

price = Money.of("19.99", "USD")
tax   = price.percent("8.25")      # Money('1.65', 'USD')
total = price + tax                # Money('21.64', 'USD')

# split three ways with not one cent lost
total.split(3)                     # [Money('7.22','USD'), Money('7.21','USD'), Money('7.21','USD')]
# ...which sums back to exactly 21.64
  • Zero dependencies. Pure standard library (decimal, int). Nothing to audit but this repo.
  • Floats are rejected, loudly. Money.of(10.99, "USD") raises. Pass a string.
  • Penny-perfect allocation. split() / allocate() always sum back to the original.
  • Real currencies. ISO 4217 exponents baked in — 2 for dollars, 0 for yen, 3 for dinars, 8 for BTC. Register your own (loyalty points, in-game gold).
  • Explicit rounding. HALF_UP, banker's HALF_EVEN, UP, DOWN, CEILING, FLOOR.
  • Serializes cleanly. to_dict() / from_dict() round-trip through JSON with the exact integer preserved.

Install

pip install larzmoney

Why not just use floats (or Decimal)?

>>> 0.1 + 0.2
0.30000000000000004        # floats: money silently drifts
>>> from decimal import Decimal
>>> Decimal("0.10") + Decimal("0.20")
Decimal('0.30')            # better — but nothing stops you splitting $10 three
                           # ways and losing a cent, or adding USD to EUR

Decimal fixes the representation but not the domain: it won't stop you mixing currencies, it won't round to the right number of places for the currency, and it won't split a bill without leaking fractions. larzmoney is Decimal done up as actual money.

The allocation guarantee

The reason this library exists. Splitting money is not division — you can't hand someone a third of a cent. allocate distributes the remainder fairly so the parts always sum back to the original:

>>> from larzmoney import Money, sum_money
>>> parts = Money.of("10.00", "USD").split(3)
>>> parts
[Money('3.34', 'USD'), Money('3.33', 'USD'), Money('3.33', 'USD')]
>>> sum_money(parts) == Money.of("10.00", "USD")
True

>>> Money.of("0.05", "USD").allocate([3, 7])     # weighted split
[Money('0.02', 'USD'), Money('0.03', 'USD')]

Use it for invoice line splits, marketplace fee/seller payouts, tax apportionment, group bills — anywhere the parts must reconcile to the whole.

Rounding modes

from larzmoney import Money, HALF_EVEN

Money.of("1.005", "USD")                       # 1.01  (HALF_UP, the default)
Money.of("1.005", "USD", rounding=HALF_EVEN)   # 1.00  (banker's rounding)
Money.of("10.00", "USD").multiply("0.0825")    # 0.83  sales tax, rounded

Modes: HALF_UP, HALF_EVEN, HALF_DOWN, UP, DOWN, CEILING, FLOOR (pass the constant or its name, e.g. rounding="HALF_EVEN").

Currency conversion

larzmoney doesn't fetch rates (that would break zero-dependency and bake in an opinion). You supply the rates you trust; it does the exact arithmetic:

from larzmoney import Money, ExchangeRates

fx = ExchangeRates("USD").add("EUR", "0.92").add("GBP", "0.79")
fx.convert(Money.of("100", "USD"), "EUR")      # Money('92.00', 'EUR')
fx.convert(Money.of("100", "EUR"), "GBP")      # cross-rated through USD

Custom currencies

from larzmoney import Money, Currency, register

register(Currency("GOLD", exponent=0, symbol="🪙", name="Game Gold"))
Money.of("500", "GOLD").format()               # '🪙500'

API at a glance

Money.of(amount, ccy, rounding=HALF_UP) build from a human amount ("10.99")
Money(minor_units, ccy) / Money.from_minor(...) build from cents
Money.zero(ccy) zero in a currency
.minor_units / .amount exact int / exact Decimal
+ - * /, .multiply(), .divide(), .percent() arithmetic (scalars, not floats)
.split(n) / .allocate(ratios) penny-perfect splitting
< <= > >= ==, .sign(), .is_zero/positive/negative() comparison
.format(symbol=, grouping=, code=) display string
.to_dict() / Money.from_dict() JSON-safe round-trip
sum_money(iterable, currency=None) safe sum
ExchangeRates(base).add(ccy, rate).convert(m, to) conversion

Tests

python -m unittest discover -s tests -v      # 68 tests, zero dependencies

The Larz stack

Part of a family of pure-Python, zero-dependency building blocks:

  • larz — money-native web framework
  • larzchain — from-scratch PoW blockchain
  • larzmoney — this library

License

MIT © larz-scripter

Metadata

Release files for larzmoney 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for larzmoney 0.1.0
File Size Uploaded Provenance
larzmoney-0.1.0.tar.gz 16.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for larzmoney 0.1.0
File Interpreter ABI Platform
larzmoney-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 31.2 kB

Release files / larzmoney-0.1.0.tar.gz

Download URL larzmoney-0.1.0.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
cb5b3146ee7becfda67f126a7babf1d8dc5144ce558adbe1ccb0d426a5d53002
BLAKE2b-256 checksum
How to use checksums
ca2250a40295539780e66d0807237e025e9d211a63add473ab8bc9e5fddadecf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / larzmoney-0.1.0-py3-none-any.whl

Download URL larzmoney-0.1.0-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
290c4face19a1b92ff4748d86952eea8c78d55df746ddec5300463e97d35fa2f
BLAKE2b-256 checksum
How to use checksums
c76339d8e1e4fad647f4d7214e70943034afa8dae8fca838ce0798257380da2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for larzmoney 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page