How to round a number to significant figures in Python
Codemia Knowledge Hub ·
Compute the number's scale with log10, then pass round the matching number of decimal places. Significant figures depend on the magnitude of the number, so the rounding has to account for where the first non-zero digit appears.
A reusable round_sig function
Output:
This works because the exponent from log10 tells you how many places the first significant digit is from the decimal point.
Why round(x, 3) is not three significant figures
Rounding to decimal places answers a different question:
That rounds to three places after the decimal point. Significant-figure rounding changes with the size of the number, while decimal-place rounding does not, which is why a dedicated function is needed.
Formatting with g for display
If you want a display string rather than a rounded float, use the g format specifier, which works in significant digits:
Formatting returns text, while round_sig returns a number. If the goal is presentation only, g formatting is simpler than a numeric helper.
Zero and negative numbers
Zero needs explicit handling because log10(0) is undefined and raises a math-domain error. That is why the helper checks for zero first.
Negative numbers work because the magnitude is computed with abs:
The sign is preserved, but the scale calculation uses the absolute value.
Trailing zeros are not preserved in floats
Numeric values do not remember display formatting, so 12.300 may appear as 12.3 unless you format it as text.
Decimal for exact decimal workflows
For scientific display or financial-style decimal work where you want tighter control, Decimal can help. For many ordinary tasks, round plus log10 is enough. Keep the goal clear, because these are related but different needs:
- numeric rounding result
- formatted display string
- exact decimal workflow
Rounding many values
Apply the helper in a list comprehension or array workflow rather than repeating the formula inline. One helper keeps the intent readable, makes zero handling consistent, and lets you unit test the rounding logic once.
Related reading
- How to round to 2 decimals with Python?
- How to route a chain of tasks to a specific queue in celery?
- How to run a python script from IDLE interactive shell?
- How to run an .ipynb Jupyter Notebook from terminal?
- How to run Anaconda Python on sudo
- How to run Django's test database only in memory?
- How to run Flask with Gunicorn in multithreaded mode
- How to run multiple Python versions on Windows
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.