# [Tutor] Rounding to n significant digits?

Tim Peters tim.peters at gmail.com
Fri Jul 2 23:18:55 EDT 2004

```[Dick Moores]
> ...
> No, the 3 examples I gave are exactly what I want:
> float = 123.456789, n = 4, returns 123.5
> float = .000000123456789, n = 2, returns .00000012
> float = 123456789, n = 5, returns 123460000

I expect the easiest way to do this in Python is to convert to string
using an %e format, then convert that back to float again.  Like this:

"""
def round_to_n(x, n):
if n < 1:
raise ValueError("number of significant digits must be >= 1")
# Use %e format to get the n most significant digits, as a string.
format = "%." + str(n-1) + "e"
as_string = format % x
return float(as_string)

print round_to_n(123.456789, 4)
print round_to_n(.000000123456789, 2)
print round_to_n(123456789, 5)
"""

That displays

123.5
1.2e-007
123460000.0

Be sure to the read the appendix on floating-point issues in the
Python Tutorial too!  You clearly have decimal digits in mind, but
that's not how your computer's floating-point hardware works.  The
appendix talks about the consequences of that.

```