[Tutor] Rounding to n significant digits?

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Jul 4 13:14:11 EDT 2004


Hi Gregor,

> def eng_round_to_n(x,n):
>     man,exp = round_to_n(x, n).split("e") # contains error-handling
>     sh=int(exp)%3
>     return "%.*fe%+04i" % (n-1-sh,float(man)*10**sh,int(exp)-sh)

Eek! Are you sure that last line is Python and not Perl?   
Winner of this months obfuscated tutor code I think...
I'll take your word for it that it works.

;-)

Alan G



More information about the Tutor mailing list