Python
significant figures
rounding numbers
programming tutorial
Python tips

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

python
1import math
2
3
4def round_sig(x: float, sig: int = 3) -> float:
5    if x == 0:
6        return 0.0
7    return round(x, sig - int(math.floor(math.log10(abs(x)))) - 1)
8
9
10print(round_sig(12345.6, 3))
11print(round_sig(0.0123456, 3))

Output:

text
12300.0
0.0123

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:

python
print(round(12345.6, 3))

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:

python
1x = 12345.6
2print(f"{x:.3g}")
3
4x = 0.0123456
5print(f"{x:.3g}")

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:

python
print(round_sig(-9876.5, 2))

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.


Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms