On Thu, Oct 21, 2010 at 15:44, Richard D. Moores <rdmoo...@gmail.com> 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'?

(Also, please suggest a better name for this function.)

See this latest revision at <http://tutoree7.pastebin.com/K9ikqNFC>.

Dick
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to