pycents is a strongly typed Python library for representing and manipulating monetary values with ISO 4217-compliant currencies and explicit rounding semantics and rich formatting options.
-
ISO 4217 compliance — currencies are represented with their standardized codes and minor-unit definitions, while Money uses integer minor units for exact monetary representation.
-
Explicit rounding — PyCents use a dual-type architecture for exact and high-precision operations:
MoneyThe core immutable type that stores exact monetary values as integer minor units (e.g., cents).UnroundedMoney: A high-precision type designed for complex intermediate calculations without premature rounding.- Seamless Integration: Both types work together fluidly in arithmetic. Calling
.round()on anUnroundedMoneyinstance explicitly converts it back into standard Money.
Features
- 100% Test Coverage & Property-Based Testing: Fully covered and rigorously verified using Hypothesis to guarantee mathematical invariants.
- ISO 4217 currency definitions
- Immutable
Moneytype - Precise decimal arithmetic
- Explicit rounding semantics.
- Locale-aware currency formatting
- Pluggable formatting backends
- Custom format specification
Example
from decimal import Decimal
from pycents import Money, UnroundedMoney, formatting, allocation as alloc
# Use a locale-aware formatting backend
formatting.use_backend("babel") # Other options are: 'std'(default) and 'icu'
rent = Money.from_major(121_555, "USD")
# the '.2c' format field is for displaying in compact notation
# while retaining 2 decimals
print(f"{rent:.2c}") # Output: $121.56K
# Apply an 8.875% municipal property tax
tax_rate = Decimal("0.08875")
# `tax` is not a Money instance!
tax = rent * tax_rate
assert isinstance(tax, UnroundedMoney)
# At the end the arithmetic pipeling, call round to get a `Money` instance
total = (rent + tax).round()
# allocate the rent among three clients according to a given ratios
shares = alloc.allocate(total, [1000, 2000, 3000])
result = ", ".join(f"{share:.2c}" for share in shares)
print("Allocated Shares:", f"[{result}]")
# Output: Allocated Shares: [$22.06K, $44.11K, $66.17K]
Documentation
Documentation and API reference are available at pycents.readthedocs.io. This is still a work in progress, some guide are complete (Quickstart, formatting and allocation) but still need to be polished.
License
pycents is distributed under the MIT License.
See the LICENSE file for details.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pycents-1.1.0.tar.gz.
File metadata
- Download URL: pycents-1.1.0.tar.gz
- Upload date:
- Size: 140.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cce47ada2dfc3f8bd74e04c84b406c6c5812ef27f4521a27c9980c0f74c09dbc
|
|
| MD5 |
3726770cbe6f5acfbd882c4c77a0f02d
|
|
| BLAKE2b-256 |
b19234c62efd12d7ae5c3e20e6d71a4cbd89f9f100a60276bd262c8aebf713c1
|
File details
Details for the file pycents-1.1.0-py3-none-any.whl.
File metadata
- Download URL: pycents-1.1.0-py3-none-any.whl
- Upload date:
- Size: 28.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
18504694efd00751aa62bef6c21bb629ec8b8eec14c03b43eebb674df38dfca7
|
|
| MD5 |
550ee9e11e43b2df123c4c40f8a60eaa
|
|
| BLAKE2b-256 |
1bfbdb529363ccbcf89a8c0a0a9d8d1d4bf65169b84c98f6447c945107485236
|