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

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.0b1.tar.gz (189.2 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.0b1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: monetils-0.2.0b1.tar.gz
  • Upload date:
  • Size: 189.2 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.0b1.tar.gz
Algorithm Hash digest
SHA256 58645989afabd07d1a4b41f02c3d101d25d4b6657a3f530b96cc2212580747d7
MD5 378e4f723450ec5efe5bfbe47f8ec654
BLAKE2b-256 d962bd308e0b92f58c0cdb1a5f6f985535f6059911c4f0e760aa1ea58d89668d

See more details on using hashes here.

Provenance

The following attestation bundles were made for monetils-0.2.0b1.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.0b1-py3-none-any.whl.

File metadata

  • Download URL: monetils-0.2.0b1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 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.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 6c4b55e8976745c14144b3c6ccbbad8d4d024454b7f3923c038bdc73d6ffce1c
MD5 e269684747579e5ddf49ac8fca0c262d
BLAKE2b-256 8d293d1af02ed7831ba2f2d09d8f8d855f37219603cde088e6d0d174b796c050

See more details on using hashes here.

Provenance

The following attestation bundles were made for monetils-0.2.0b1-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.0b1 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