[Tutor] Rounding to n significant digits?

Dick Moores rdm at rcblue.com
Sun Jul 4 03:12:17 EDT 2004


Tim Peters wrote at 12:49 7/3/2004:
>Then it's time to learn about one of the more obscure features of
>string formats:  if you put an asterisk in a string format where a
>precision specifier is expected, the actual precision to use will be
>taken from the argument tuple.  I realize that's confusing; that's why
>I called it obscure <wink>.  It should be clearer from this rewrite of
>your rewrite of my original round_to_n function:
>
>def round_to_n(x, n):
>     if n < 1:
>         raise ValueError("number of significant digits must be >= 1")
>     return "%.*e" % (n-1, x)

Nice. Thanks very much for all your help.

Dick Moores





More information about the Tutor mailing list