Short Answer
Quick answer: To round to significant figures in Python without floating-point surprises, convert the value to decimal.Decimal from a string, set a sufficient context precision, compute the target exponent from Decimal.adjusted(), and use quantize() with the rounding mode your standard or instructor requires. Do not rely on round() as a significant-figures formatter. Python’s round() is a decimal-place or tens-place tool with round-half-even tie-breaking, and binary floats cannot represent most base-10 fractions exactly.
Rule Statement
Significant figures are the digits in a measured value that carry meaningful information about its precision. Rounding to n significant figures means keeping the first n meaningful digits and replacing the rest with zeros or dropping them, while preserving the magnitude of the value. For a nonzero value x, the target power of ten is e = floor(log10(|x|)) − n + 1. The rounded value is the nearest multiple of 10e, using the tie rule you have chosen.
Python is not the standard; it is a tool. Choose the rounding convention required by your lab, standard, or instructor, then encode it explicitly in code.
The general digit rule is familiar: if the first dropped digit is greater than 5, round up; if it is less than 5, round down. If it is exactly 5 followed by nonzero digits, round up. If it is exactly 5 with nothing but zeros after it, the convention matters: half-up, half-even, half-down, and truncation all give different answers. See our rounding rules and Why Sig Figs Are an Approximation for deeper background.
Software Behavior Note: Python Floats, round(), Decimal, and Fraction
Python’s float is IEEE 754 binary64. Most decimal fractions, including 0.1, 2.675, and 0.0012345, have no exact binary representation. That is why 0.1 + 0.2 is not exactly 0.3, and why round(2.675, 2) returns 2.67 rather than 2.68. The stored float is slightly less than 2.675, so the tie never occurs.
Python’s built-in round() has two additional surprises for metrology work:
- Tie rule: for exact binary ties,
round()uses round-half-even, often called banker’s rounding. This is useful in finance and some standards, but it is not always the classroom or laboratory convention. - Not significant figures:
round(x, n)rounds to n decimal places, not n significant figures.round(12345, -2)rounds to hundreds, not to three significant figures in a general way.
For decimal-safe work, use Decimal constructed from a string. The expression Decimal(str(x)) preserves the decimal value the user intended, whereas Decimal(x) converts the already-corrupted binary float. A robust significant-figures function looks like this:
from decimal import Decimal, localcontext, ROUND_HALF_UP
def round_sig(x, sig=3, rounding=ROUND_HALF_UP):
d = Decimal(str(x))
if d.is_zero():
return Decimal('0')
with localcontext() as ctx:
ctx.prec = max(28, len(d.as_tuple().digits) + sig + 5)
exponent = d.adjusted() - sig + 1
rounded = d.quantize(Decimal(1).scaleb(exponent), rounding=rounding)
# handle carry such as 999.9 -> 1000
exponent2 = rounded.adjusted() - sig + 1
if exponent2 != exponent:
rounded = d.quantize(Decimal(1).scaleb(exponent2), rounding=rounding)
return rounded
For exact rational arithmetic, fractions.Fraction can avoid decimal representation issues, but it does not by itself define significant-figure output or trailing-zero display. In practice, Decimal is usually the right tool for measurement-style significant-figure rounding.
Worked Examples
1. 12,345 to 3 significant figures
Decimal('12345').adjusted() is 4. The target exponent is 4 − 3 + 1 = 2. Quantize to 1E+2, giving Decimal('1.23E+4'). Displayed as 1.23 × 104 or 12,300 with a note that the trailing zeros are placeholders unless the measurement resolution supports them.
2. 0.0012345 to 3 significant figures
The adjusted exponent is −3. The target exponent is −3 − 3 + 1 = −5. Quantize to 1E-5, giving 0.00123.
3. 2.675 to 3 significant figures
Using Decimal('2.675') with ROUND_HALF_UP gives 2.68. Using binary round(2.675, 2) gives 2.67 because the float is stored as approximately 2.674999999999999822. This is the classic floating-point surprise.
4. 2.685 to 3 significant figures
Half-up gives 2.69. Half-even gives 2.68 because the retained digit 8 is even. This example shows why conventions must be declared. A calculator or programming language that does not expose its tie rule can silently disagree with a specification.
5. 999.9 to 3 significant figures
The initial target exponent is 0, and rounding gives 1000. The magnitude has carried, so re-quantize to 1E+1 to preserve three significant digits: Decimal('1.00E+3'). Without carry handling, the result may appear to have only one significant figure.
Counter-Examples
round(2.675, 2)→ 2.67: This is not a significant-figure rule; it is binary representation plus round-half-even.Decimal(2.675): This converts the binary float, producingDecimal('2.67499999999999982236431605997495353221893310546875'). UseDecimal(str(2.675))instead.round(12345, -3): This rounds to thousands, not to a general number of significant figures. It fails as soon as the magnitude changes.round(1.20, 1)→ 1.2: This loses the trailing zero that may carry significant-figure meaning. Preserve display precision separately from numeric value.- Double rounding: Rounding 2.4445 to 2 significant figures by first rounding to 2.445 then to 2.4 can produce a different result than rounding directly. See Double Rounding Error.
Convention Comparison Table
| Convention | Exact tie behavior | Typical use | Python Decimal mode |
|---|---|---|---|
| Half-up | Always round away from zero on exact 5 | School arithmetic, many lab reports | ROUND_HALF_UP |
| Half-even | Round to nearest even retained digit | ASTM E29, ISO 80000, finance, floating-point hardware | ROUND_HALF_EVEN |
| Half-down | Always round toward zero on exact 5 | Specialized conservative estimates | ROUND_HALF_DOWN |
| Ceiling | Always round up | Tolerance limits, safety margins | ROUND_CEILING |
| Floor | Always round down | Lower bounds, truncation-like limits | ROUND_FLOOR |
| Truncation | Discard extra digits | Some digital instruments and intermediate displays | ROUND_DOWN |
Standards Citation
Significant-figure rounding is not a single universal algorithm. Standards define acceptable practice for specific domains:
- ASTM E29-22, Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications, Sections 6–7, covers rounding and significant-digit use in test data. It is widely cited when deciding whether a measured value conforms to a specification.
- ISO 80000-1:2022, Quantities and units — Part 1: General, Clause 7.3, gives rounding conventions, including the treatment of exact ties. The half-even convention is common in metrology and standardization.
- JCGM 100:2008, the Guide to the expression of uncertainty in measurement (GUM), Clause 7.2.6, advises against excessive digits when reporting estimates and uncertainties. Many fields report uncertainty to one or two significant digits.
- NIST SP 811, Guide for the Use of the International System of Units (SI), Section 7.4, discusses rounding values and the importance of matching the reported digits to the measurement uncertainty.
Always record the rounding convention with the data. A number rounded half-up can differ from the same number rounded half-even by one unit in the last retained place.
Common Mistakes
- Using
round()for significant figures. It rounds to decimal places, not significant digits, and its tie rule is not configurable. - Using
Decimal(float). This preserves the binary error instead of the intended decimal value. UseDecimal(str(x))orDecimal('2.675'). - Forgetting context precision. Decimal operations are affected by the current context. Increase precision locally before quantizing.
- Assuming trailing zeros are significant. 1200 may have two, three, or four significant figures. See Ambiguous Trailing Zeros.
- Mixing conventions silently. Half-up and half-even are both defensible, but only one may match your specification.
- Rounding intermediate results. Keep full precision through calculations and round only the final reported value, unless a standard says otherwise.
- Ignoring zero. Zero has no nonzero significant digits; its precision must be defined by context, uncertainty, or measurement resolution.
Quick Reference Table
| Task | Recommended Python | Note |
|---|---|---|
| Create exact decimal | Decimal(str(x)) |
Do not use Decimal(float) for intended decimal ties. |
| Round to n sig figs | round_sig(x, n, ROUND_HALF_UP) |
Compute exponent with adjusted(); handle carry. |
| Use half-even | rounding=ROUND_HALF_EVEN |
Common in ASTM, ISO, and IEEE 754. |
| Use half-up | rounding=ROUND_HALF_UP |
Common in education and general reporting. |
| Preserve trailing zeros | quantize(Decimal('1E-3')) |
Numeric value and displayed precision are separate concerns. |
| Exact rational work | Fraction('2.675') |
Useful for exact arithmetic, but define your rounding rule separately. |
Sources & Further Reading
- ASTM E29-22, Standard Practice for Using Significant Digits in Test Data to Determine Conformance with Specifications, ASTM International.
- ISO 80000-1:2022, Quantities and units — Part 1: General, Clause 7.3.
- JCGM 100:2008, Evaluation of measurement data — Guide to the expression of uncertainty in measurement (GUM), Clause 7.2.6.
- NIST Special Publication 811, Guide for the Use of the International System of Units (SI), Section 7.4.
- Python Software Foundation, decimal — Decimal fixed point and floating point arithmetic.
This site is a precision and rounding reference, not just a calculator. The calculator is designed to apply these rules accurately, but the surrounding reference material explains why the rules exist, how standards differ, and where floating-point implementations can mislead. For related topics, see Rounding Methods, Banker’s Rounding, and Python.
FAQ
Why does Python's round(2.675, 2) return 2.67?
Because 2.675 is stored as a binary float slightly less than 2.675. Python rounds the actual stored value, not the decimal literal. Use Decimal('2.675') when the decimal tie matters.
How do I round to significant figures in Python?
Convert to Decimal from a string, compute exponent = d.adjusted() - sig + 1, and call d.quantize(Decimal(1).scaleb(exponent), rounding=...). Handle zero and carry cases explicitly.
Does Python's round() use banker's rounding?
Yes, for exact ties Python 3 uses round-half-even. However, exact decimal ties are rare in binary floating point, so the visible result often depends on the binary representation rather than the tie rule.
Should I use Decimal or Fraction?
Use Decimal for decimal measurement values and significant-figure formatting. Use Fraction when you need exact rational arithmetic and will apply a rounding rule at the end.
How do I preserve trailing zeros as significant?
Store or display the rounded value with the correct exponent, such as Decimal('1.20E+2') or a formatted string. A numeric value alone cannot always communicate whether trailing zeros are significant.

Leave a Reply