How to round a number to significant figures in Python
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Rounding to significant figures is different from rounding to a fixed number of decimal places. Significant figures are based on the magnitude of the number, so the rounding logic has to account for where the first non-zero digit appears. In Python, the usual approach is to compute that scale from log10 and then call round with the appropriate number of decimal places.
A Reusable Function
A common implementation looks like this:
Output:
This works because the exponent from log10 tells you how many places the first significant digit is away from the decimal point.
Why Decimal Places Alone Do Not Work
Rounding to decimal places answers a different question:
That rounds to three places after the decimal point, not to three significant figures. Significant-figure rounding changes with the size of the number, while decimal-place rounding does not.
That is why a dedicated function is needed.
Formatting for Display
Sometimes you want a display string rather than a rounded float value. Python formatting can help:
The g format specifier uses significant digits. This is very convenient when the goal is display or reporting instead of numeric reuse.
A key difference is that formatting returns text, while the earlier helper returns a numeric value.
Edge Cases: Zero and Negative Numbers
Zero needs explicit handling because log10(0) is undefined. That is why the helper function checks for zero first.
Negative numbers work fine as long as the magnitude is computed with abs:
The sign is preserved, but the scale calculation uses the absolute value.
Decimal for Exact Decimal Workflows
If you are doing scientific display or financial-style decimal workflows and want tighter control over decimal behavior, Decimal can be useful. But for many ordinary Python tasks, the round plus log10 approach is enough.
The important thing is to keep the goal clear:
- numeric rounding result
- formatted display string
- exact decimal workflow
Those are related, but not identical needs.
Applying the Idea to Many Values
If you need to round many values, apply the helper in a list comprehension or array workflow rather than repeating the formula inline everywhere. A small helper function keeps the intent readable and makes zero-handling consistent.
This also makes unit testing easier, because you can test the rounding logic once and then reuse it wherever significant-figure output is needed. That consistency matters in reporting code.
Common Pitfalls
One common mistake is confusing significant figures with decimal places. round(x, 3) does not mean three significant figures unless the number happens to have that shape already.
Another mistake is forgetting to handle zero before using log10. That leads to a math-domain error.
Developers also sometimes expect a float result to preserve trailing zeros visibly. Numeric values do not remember display formatting, so 12.300 may appear as 12.3 unless you format it as text.
Finally, if the goal is presentation only, using string formatting with g can be simpler than building a reusable numeric helper.
Summary
- Significant-figure rounding depends on the magnitude of the number, not just decimal places.
- A common Python solution uses
log10,floor, androundtogether. - Handle zero explicitly before computing the scale.
- Use
f"{x:.3g}"when the goal is formatted display rather than a numeric result. - Keep numeric rounding and presentation formatting conceptually separate.

