Jim Knoll
Jim Knoll

Reputation: 115

Formatting float with zero padded and implied decimal

is there a mini language way for format like this?

cram a float into 9999^99 zero padded ^ is a implied decimal.

I was doing this

for x in (450.5, 50.0, 50.0043, 9999.989,0.5):
    print ('{0:06}'.format(round(x,2)).replace('.','')+'000')[0:6]

but felt a little dirty afterwords... so I changed it...

I know I could rip apart the float and put it back together

for x in (450.5, 50.0, 50.0043, 9999.989,0.5, 4):
    print '{0:04}{1:0<2}'.format(int(x),int(x % 1 * 100))

this did not make me feel much better.
I know what I am doing but only because I wrote it...

anyone have a great way that is concise and clear? Or is this code clear enough?

Upvotes: 3

Views: 518

Answers (1)

cmd
cmd

Reputation: 5830

  print "{:06.0f}".format(round(100*x))

Upvotes: 2

Related Questions