sigfig is a Python library (current version 1.3.19) designed for precise rounding of numbers based on significant figures, decimal places, or uncertainty. It also provides versatile formatting options and can interpret various numeric input types (e.g., strings, floats, Decimals). The library aims to provide expected rounding results in scientific and engineering contexts, often differing from Python's built-in `round()` function. It is actively maintained with releases as recent as March 2025.
pip install sigfigVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import `sigfig.round` and use it for common rounding operations: by significant figures, by decimal places, and by uncertainty. It also highlights the recommendation to pass numbers as strings to maintain precision.
Pass numbers to `sigfig.round()` as strings (e.g., `'10.00'`) or `decimal.Decimal` objects when precise control over significant figures and trailing zeros is critical.
Import `sigfig` with an alias (e.g., `import sigfig as sf`) and call its functions explicitly (e.g., `sf.round()`) to avoid conflicts.
Ensure the input number has sufficient precision (e.g., by providing it as a string with enough digits, like `'1.000'`). If you intentionally want to format a number to a minimum number of significant figures, you can suppress the warning with `warn=False` (e.g., `round(1, sigfigs=3, warn=False)`).
Provide the number with sufficient precision (e.g., as a string `'1.00'`). Alternatively, if the intent is to enforce a minimum number of significant figures, suppress the warning: `from sigfig import round; round(1, sigfigs=3, warn=False)`.
Always pass numbers as strings (e.g., `'10.00'`, `'1200.'`) or `decimal.Decimal` objects to `sigfig.round()` to preserve all significant digits, including trailing zeros. Example: `from sigfig import round; round('10.00', sigfigs=3)`.