On Jul 31, 7:42 pm, "Dr. Phillip M. Feldman" <pfeld...@verizon.net>
wrote:
> This was very close to what I wanted.  Thanks!  My final code looks like
> this:
>
> def num2str(x,f=4):
>    """Convert x (int or float) to a string with f digits to right of
>    the decimal point.  f may be zero or negative, in which case the decimal
>    point is suppressed."""
>    s= str(round(x,f))
>    if f<=0:
>       # Delete decimal point:
>       i= s.find('.')
>       if i>0: s= s[:i]
>    return s


Is it *really* necessary to create a named function for something as
trivial as this?

>>> import math
>>> '%0.3f' %math.pi
'3.142'
>>> '%d' %math.pi
'3'

Seems string formatting is more "pythonic" -- opps, now why did i have
to go and say that... :)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to