On Fri, Oct 22, 2010 at 9:42 AM, Richard D. Moores <[email protected]> wrote: > On Thu, Oct 21, 2010 at 15:44, Richard D. Moores <[email protected]> wrote: >>> If you want to control the number of decimal places in the string >>> formatting do something like: >>> >>>>>> i = 3 >>>>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) >>> NEW LOW: 81.750 at 22:55:13 >>>>>> i = 6 >>>>>> print ("NEW LOW: %%.%sf at %%s" % i) % (lowz, timestamp) >>> NEW LOW: 81.750000 at 22:55:13 >> >> Thanks very much for the string formatting instruction. > > So I wrote a function: > > def float2n_decimals(floatt, n): > """ > Given a float (floatt), return floatt to n decimal places. > > E.g., with n = 2, 81.34567 -> 81.35 > """ > return ("%%.%sf" % n) % floatt > > which works fine, but a question remains: n is an integer. Why the 's' > in '%sf'?
Right here: http://docs.python.org/release/2.5.2/lib/typesseq-strings.html > > (Also, please suggest a better name for this function.) > > See this latest revision at <http://tutoree7.pastebin.com/K9ikqNFC>. > > Dick > _______________________________________________ > Tutor maillist - [email protected] > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ Tutor maillist - [email protected] To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
