Floating numbers and str

Jeffrey Schwab jeff at schwabcenter.com
Wed Nov 9 14:56:41 EST 2005


Tuvas wrote:
> I would like to limit a floating variable to 4 signifigant digits, when
> running thorugh a str command. Ei,
> 
> 
> 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? Thanks!

%d for a floating-point type?  Is that right?

Anyway, ITYW:

	"%.4g" % x

E.g:

 >>> x=.13241414515
 >>> y="%.4g something here" % x
 >>> print y
0.1324 something here
 >>>



More information about the Python-list mailing list