{"id":7716,"library":"sigfig","title":"sigfig - Scientific Rounding","description":"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.","status":"active","version":"1.3.19","language":"en","source_language":"en","source_url":"https://github.com/drakegroup/sigfig.git","tags":["rounding","significant figures","decimals","uncertainty","scientific notation","precision","mathematics"],"install":[{"cmd":"pip install sigfig","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Used for internal data structures and is listed as a dependency for manual installations.","package":"sortedcontainers","optional":false}],"imports":[{"note":"The `sigfig.round` function provides different (often more scientifically accurate) rounding behavior than Python's built-in `round()`, especially concerning decimal places and significant figures. The built-in `round(0.25, 1)` yields `0.2`, while `sigfig.round(0.25, decimals=1)` yields `0.3`.","wrong":"round(0.25, 1)","symbol":"round","correct":"from sigfig import round"}],"quickstart":{"code":"from sigfig import round\n\n# Round to 4 significant figures\nresult_sigfigs = round('123.456', sigfigs=4)\nprint(f\"Rounded to significant figures: {result_sigfigs}\")\n\n# Round to 2 decimal places\nresult_decimals = round('3.14159', decimals=2)\nprint(f\"Rounded to decimal places: {result_decimals}\")\n\n# Round by uncertainty\nresult_uncertainty = round('3.14159', uncertainty=2)\nprint(f\"Rounded by uncertainty: {result_uncertainty}\")","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"Python's native float type can lead to precision loss, especially with trailing zeros (e.g., `10.0` might internally be `10.0` with 2 sigfigs, not `10.00` with 3). This can cause `sigfig` to misinterpret the intended precision.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Import `sigfig` with an alias (e.g., `import sigfig as sf`) and call its functions explicitly (e.g., `sf.round()`) to avoid conflicts.","message":"Importing `from sigfig import round` directly overwrites the built-in `round()` function. This can lead to unexpected behavior if other parts of your code rely on the standard `round()` behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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)`).","message":"The library may issue a `UserWarning` if you request more significant figures than the input number inherently possesses, indicating potential 'invented numbers'.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"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)`.","cause":"You are attempting to round a number to more significant figures than it inherently possesses or than `sigfig` can infer from its type. For example, passing the integer `1` and requesting `sigfigs=3`.","error":"UserWarning: warning: X significant figures requested from number with only Y significant figures"},{"fix":"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)`.","cause":"Python's `float` type often discards trailing zeros that are significant in scientific notation (e.g., `float('10.00')` might be `10.0`). When `sigfig` receives such a float, it loses the original precision information.","error":"Incorrect rounding or significant figure count for numbers with trailing zeros (e.g., 10.0, 1200)"}]}