Felipe
Felipe

Reputation: 37

floating number string formatting in python

I have a float let say 8.8 and I want to format it into 0008.800

I read this http://docs.python.org/2/library/stdtypes.html#string-formatting

If I do

'%06g'%(8.8)

I get 0008.8

but I still don't know how to include the other decimals

Upvotes: 0

Views: 148

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 251186

Use %f not %g:

>>> '%08.3f'%(8.8)
'0008.800'

Where 8 is the width and 3 is the precision.

With new style string formatting:

>>> "{:08.3f}".format(8.8)
'0008.800'

Upvotes: 2

Related Questions