On Fri, Oct 22, 2010 at 11:27, Alan Gauld <alan.ga...@btinternet.com> wrote: > > "Richard D. Moores" <rdmoo...@gmail.com> wrote > >> return ("%%.%sf" % n) % floatt >> >> which works fine, but a question remains: n is an integer. Why the 's' >> in '%sf'? > > Its arbitrary. You could use %d just as easily. > %s will put the numner into the string, so will %d. > %d is probably a bit more picky about what it inserts - so might > actually be a better choice in this case since > > float2n(1.234567, 3.5) > > Doesn't work as you might hope... but changing it to use %d might - > depending > on your definition of better! And what you might hope! :-) > > OTOH %s handles this better: > > float2n(1.234567, '3')
If I use n = 2.5 and replace %s with %d, my function is 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 ("%%.%df" % n) % floatt This works (the d converts the 2.5 to an int, 2), but I get a deprecation warning: "DeprecationWarning: integer argument expected, got float rate = rate1 = round(float(rate2),n)" Of course, n = 2.5 makes no sense in my script. Dick _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor