Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

monetils

Tests Coverage License PyPI TestPyPI
A utility library for representing monetary amounts in Python.

Installation

pip install monetils

Usage

monetils provides BTC and USD classes for representing monetary amounts exactly, without floating-point rounding artifacts.

Construction and formatting

from monetils import BTC, USD

usd = USD(1234.5)
print(str(usd))  # "1234.50"

btc = BTC(1)
print(str(btc))  # "1.00000000"

# Construct from a non-default unit
half_btc = BTC.sat(50_000_000)
print(str(half_btc))  # "0.50000000"

nineteen_ninety_nine = USD.cent(1999)
print(str(nineteen_ninety_nine))  # "19.99"

Arithmetic

from monetils import BTC, USD, CurrencyMismatchError

total = USD(10.50) + USD(5.25)  # USD(15.75)

# A plain number used with +/- is treated as an amount in the currency's base unit
one_more_btc = BTC(1) + 1  # BTC(2)

# A plain number used with */÷ is a dimensionless scaling factor
doubled = BTC(1) * 2  # BTC(2)

# Mixing currencies raises CurrencyMismatchError instead of a silent wrong result
try:
    BTC(1) + USD(1)
except CurrencyMismatchError:
    print("rejected as expected")

Unit conversion

from monetils import BTC

one_btc = BTC(1)
one_btc.to("sat")  # Decimal("100000000")
one_btc.sats  # 100000000

msat_value = one_btc.to("msat")
BTC.msat(msat_value) == one_btc  # True — lossless round trip

Pydantic support

BTC and USD work directly as Pydantic v2 model fields — no wrapper type needed. This requires pydantic>=2.0,<3 to be installed in your own project; monetils does not depend on it (it stays a zero-runtime-dependency library).

from pydantic import BaseModel
from monetils import BTC, USD

class Invoice(BaseModel):
    price: USD
    fee: BTC | None = None

# Accepts a plain number/string, or an existing BTC/USD instance
invoice = Invoice(price=19.99, fee="0.5")

invoice.model_dump()        # {"price": USD(19.99), "fee": BTC(0.5)} — instances, unchanged
invoice.model_dump_json()   # '{"price":"19.99","fee":"0.50000000"}' — the same str() convention
Invoice.model_json_schema() # describes price/fee as JSON string properties

# Invalid input (wrong currency, unparseable value) raises a standard pydantic.ValidationError

License

This project is licensed under the terms of the MIT license.

Download files

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

Source Distribution

monetils-0.2.0b3.tar.gz (231.7 kB view details)

Uploaded Source

Built Distribution

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

monetils-0.2.0b3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file monetils-0.2.0b3.tar.gz.

File metadata

  • Download URL: monetils-0.2.0b3.tar.gz
  • Upload date:
  • Size: 231.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for monetils-0.2.0b3.tar.gz
Algorithm Hash digest
SHA256 864f30011958277ec02f79a353864ee8dbeb6e8a4702a90585b4479762e0e3a1
MD5 4c52fc8614b8739160753df965ee1daf
BLAKE2b-256 cfe00e8587b84e5b58177f9641271d382540b1edcef2a17f6423e042787be0c5

See more details on using hashes here.

Provenance

The following attestation bundles were made for monetils-0.2.0b3.tar.gz:

Publisher: publish-pypi.yml on seba3c/monetils

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

File details

Details for the file monetils-0.2.0b3-py3-none-any.whl.

File metadata

  • Download URL: monetils-0.2.0b3-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for monetils-0.2.0b3-py3-none-any.whl
Algorithm Hash digest
SHA256 b64c85b5f2ec31227bba270ade1b43d87b602566b540584837b1030b0639ceaf
MD5 62ac522b8f40af71c65118085deb7eab
BLAKE2b-256 82e0c2ec131445a7747ffc06f09720024bd4110fc5afb856176573f2cbbcbf7b

See more details on using hashes here.

Provenance

The following attestation bundles were made for monetils-0.2.0b3-py3-none-any.whl:

Publisher: publish-pypi.yml on seba3c/monetils

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

Release history Release notifications | RSS feed

This release

0.2.0b3 This release

2 files

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