figround
Significant-figures rounding and formatting with exact Decimal math and preserved trailing zeros
Install
pip install figround
Usage
from figround import (
count_sig, format_sig, format_uncertainty, round_decimal, round_sig, sig_bounds,
)
round_sig(123456, 3) # 123000.0
round_sig(2.675, 3) # 2.68, exact: no binary float drift
round_decimal(1.2, 3) # Decimal('1.20'), the exponent records the figures
round_decimal("2.5", 1, rounding="half_up") # Decimal('3'), eight rounding modes
format_sig(1.2, 3) # "1.20", trailing zeros kept
format_sig(0, 3) # "0.00"
format_sig(0.00012345, 2, sci=True) # "1.2e-04"
format_sig(123456, 4, notation="eng") # "123.5e+03"
format_sig(1000, 4, trailing_point=True) # "1000."
count_sig("0.00120") # 3
count_sig("6.022e23") # 4
sig_bounds("1200") # (2, 4), trailing zeros without a point are ambiguous
format_uncertainty(100.02147, 0.00035) # "100.02147(35)"
format_uncertainty(100.02147, 0.00035, style="pm") # "100.02147 +/- 0.00035"
Rounding and formatting accept an int, float, str or Decimal.
Integer inputs are limited to 65536 bits; use decimal text or Decimal for larger values.
Counting accepts only str or Decimal, which can record trailing zeros. Values are converted
through decimal.Decimal so the rounding matches the decimal digits you see rather
than the binary approximation: a float is read through repr, so
round_sig(2.675, 3) is 2.68, not 2.67. Rounding is half to even by default
(NIST SP 811 B.7.1, the IEEE 754 default) and every decimal rounding mode is
available. Values with an uncertainty follow the GUM (JCGM 100:2008, clause 7.2):
the uncertainty keeps two significant figures and the value is rounded to match.
Errors are typed (InvalidNumberError, InvalidFiguresError, InvalidOptionError,
AmbiguousFiguresError, UncertaintyError), all subclasses of FigroundError and
of ValueError.
Command line
figround round 2.675 3 # 2.68
figround round 123456 2 --notation sci # 1.2e+05
figround count 0.00120 1200 1200. # 3, 2, 4
figround format 100.02147 -u 0.00035 # 100.02147(35)
See GUIDE for every rule, the command options, the cited sources and
how the results differ from format(x, ".{n}g").
Why
Python's built-in format(x, ".3g") already rounds to significant figures.
Use it when its choice of notation and handling of trailing zeros fit your output.
Decimal.quantize is a good choice when you know the decimal place to round to.
figround combines explicit fixed, scientific and engineering notation, eight
rounding modes, preserved trailing zeros, and value-with-uncertainty formatting.
It reads floats through repr, so format_sig(2.675, 3) gives "2.68" while
format(2.675, ".3g") gives "2.67". It has no runtime dependencies and ships
Python type information. Counting follows the documented written-number convention;
it cannot recover precision that was lost before a number became a float.
Development
Requires Python 3.10 or newer. Install .[dev], then run scripts/check.ps1.
python scripts/fetch_upstream.py fetches the optional decimal reference archive;
set FIGROUND_UPSTREAM_DIR to store it outside the checkout. Tests skip that archive
when it is absent. See the CHANGELOG
for release changes.
License
MIT. See LICENSE.
Links
- Website: https://lianes8studio.com
- GitHub: https://github.com/JaviLianes8
- LinkedIn: https://www.linkedin.com/in/jlianes/
Metadata
Release files for figround 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| figround-1.0.0.tar.gz | 39.1 kB | Without provenance | Details for figround-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| figround-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for figround-1.0.0-py3-none-any.whl |
Total release size: 58.9 kB
Release files / figround-1.0.0.tar.gz
| Download URL | figround-1.0.0.tar.gz |
|---|---|
| Size | 39.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ceb0bdc5733551ccf79d2622538bb31ba33cfc615afa4bb8c25afcfc4e7679db
|
|
BLAKE2b-256 checksum How to use checksums |
df87a103698d7f9720e9c3e9aab6f3270878daa7b4c2fa8907c48bd28c725ad3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Release files / figround-1.0.0-py3-none-any.whl
| Download URL | figround-1.0.0-py3-none-any.whl |
|---|---|
| Size | 19.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0d6035c86e1c31c3246909b713fdd0d11b18217c6cd2c6ce81ce56262c48045f
|
|
BLAKE2b-256 checksum How to use checksums |
d62847145a32a7c1e0840d6b44e3ae603d2c3d5f831bc9ce77b99d7b1bbe178b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.2
|
Security for figround 1.0.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.