Floating numbers and str

Grant Edwards grante at visi.com
Wed Nov 9 15:05:36 EST 2005


On 2005-11-09, Tuvas <tuvas21 at gmail.com> wrote:

> I would like to limit a floating variable to 4 signifigant digits, when
> running thorugh a str command.

Sorry, that's not possible.

> x=.13241414515
> y=str(x)+" something here"
>
> But somehow limiting that to 4 sign. digits. I know that if
> you use the print statement, you can do something like %.4d,
> but how can I do this with converting the number to a string?

I don't understand what you mean about the print statement.  

If using str() isn't really a requirement, you can use the
string formatting operator "%" like this:

>>> x=.13241414515
>>> y = "%0.4g" % x
>>> y
'0.1324'

-- 
Grant Edwards                   grante             Yow!  A GRAM?? A BRAM... A
                                  at               GROOM... A BROOM... Oh,
                               visi.com            Yeh!! Wash the ROOM!!



More information about the Python-list mailing list