Skip to main content

always-decimal

A tiny Python package to safely convert floats, strings, and numbers into Decimal objects.
It solves common headaches when comparing PostgreSQL numeric values with Python floats by ensuring consistent Decimal values.


✨ Features

  • 🔒 Exact conversion from float → Decimal (no hidden rounding).
  • ⚖️ Configurable coercion: set scale, rounding mode, normalize trailing zeros.
  • 🧩 Works with float, int, str, or Decimal.
  • 🛡️ Raises clear DecimalCoercionError on invalid inputs.
  • 🐍 Compatible with Python 3.10+.
  • 📦 Easy install via uv or pip.

📦 Installation

Using uv (recommended)

uv add always-decimal

Using pip

pip install always-decimal

For local development:

uv pip install -e ".[dev]"
# or
pip install -e ".[dev]"

🚀 Usage

from always_decimal import ensure_decimal, to_decimal_exact

# 1. Exact conversion: no rounding, no scale changes
d1 = to_decimal_exact(0.1)
print(d1)
# Decimal('0.1000000000000000055511151231257827021181583404541015625')

# 2. Safe coercion with fixed scale
price = ensure_decimal(19.995, scale=2)
print(price)
# Decimal('20.00')  (ROUND_HALF_EVEN default)

# 3. String input with quantization
val = ensure_decimal("1.2345", scale=3)
print(val)
# Decimal('1.235')

# 4. Normalizing (remove trailing zeros)
norm = ensure_decimal("1.2300", scale=4, normalize=True)
print(norm)
# Decimal('1.23')

⚙️ API

to_decimal_exact(value: float | Decimal) -> Decimal

Convert a float or Decimal to a Decimal exactly:

  • float → Decimal.from_float(value)
  • Decimal → returned as-is Raises TypeError for other types.

ensure_decimal(value, *, scale=None, rounding=ROUND_HALF_EVEN, clamp_exp=True, normalize=False) -> Decimal

Convert and coerce input to Decimal.

  • scale: number of fractional digits to quantize (e.g., 2 → cents).
  • rounding: rounding mode (default: bankers rounding).
  • clamp_exp: clamp exponents to context limits.
  • normalize: trim trailing zeros.

🧪 Running Tests

make test

or directly:

pytest

📄 License

MIT


🙌 Contributing

Issues and PRs are welcome! Please format with ruff, type-check with mypy, and run pytest before submitting.

Metadata

Release files for always-decimal 1.0.1

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

Source distribution (sdist)

Source distribution for always-decimal 1.0.1
File Size Uploaded
always_decimal-1.0.1.tar.gz 39.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for always-decimal 1.0.1
File Interpreter ABI Platform
always_decimal-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 44.2 kB

Release files / always_decimal-1.0.1.tar.gz

Download URL always_decimal-1.0.1.tar.gz
Size 39.3 kB
Tags Source
SHA-256 checksum
How to use checksums
850eaafc53b02cc2c98359e9fdd6753b18e15b91f4f7cac6b59f76ca88d1cf62
BLAKE2b-256 checksum
How to use checksums
dca81a8a5ecb05cdbb6d13562f7a21a80d7c92ae4fb14cc8892dc3491e5828eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 20, 2025.

Transparency log

Release files / always_decimal-1.0.1-py3-none-any.whl

Download URL always_decimal-1.0.1-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bf606e7ba2dba22dbe82381d3a0ded9041328580b9adc3d7ceff22c18202a42a
BLAKE2b-256 checksum
How to use checksums
673665fcb697d0d6898486529fe0409a92f33e1279cfad35a2b392fb5f97a26d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 20, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release 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