Avoid rounding in new python string formatting

format, python, string

Solution

Explicitly convert the arguments to `int`s:

>>> '{:02}:{:02}:{:02}'.format(int(0.0), int(0.9), int(67.5))
'00:00:67'

BTW, you don't need to specify argument index (`{0}`, `{1}`, ..) if you use Python 2.7+, Python 3.1+ (autonumbering).

Problem

I want to replace the old string formatting behaviour with the new python string formatting syntax in my scripts, but how to avoid rounding when I deal with floats? The old version ``` print ('%02d:%02d:%02d' % (0.0,0.9,67.5)) ``` yields `00:00:67` while my (obviously wrong) translation into the new syntax ``` print ('{0:0>2.0f}:{1:0>2.0f}:{2:0>2.0f}'.format(0.0,0.9,67.5)) ``` yields `00:01:68`. How to avoid rounding here and get the old output with the new format syntax?

Original source

Related problems