PEP proposal for round(x,n) enhancement

Tim Peters tim_one at email.msn.com
Sun Sep 16 12:01:04 CEST 2001


[Christopher Smith]
> ...
> The proposal is not concerned with how numbers are represented
> internally, just with how they are printed.

Since printing supports significant-digit rounding directly (for example,
you can use format "%.3g" if you want 3 significant digits), a function
doesn't seem necessary.

> Presumably that is one thing that the round(x,n) function is for--it
> let's me through away information that I don't want.

round() predates printf-style formats in Python, and has little good use
anymore.

> ...
> 	3.0 cm (+/- 1 mm) = 1.1811023622 inches which is about 1.18
> inches; I would have to tell round to round to n=2

>>> print "%.3g" % 1.1811023622
1.18
>>>

> 	3 km (+/- 10 m) = 118110.23622 inches which is about 118000
> inches; I would have to tell round to round to the -3 digit (n=-3)

>>> print "%.3g" % 118110.23622
1.18e+05
>>>

> There should be a way to say "round to the 3rd digit from the first
> non-zero digit.

So long as you're concerned about printing, there are direct ways to do that
already.

BTW, complex numbers still have no meaning to round(), so you could use
round(x, 3j) to mean "round to 3 significant digits" <wink>.





More information about the Python-list mailing list