[Tutor] print question
Dick Moores
rdm at rcblue.com
Tue Oct 9 10:15:39 CEST 2007
At 12:58 AM 10/9/2007, Alan Gauld wrote:
>"Dick Moores" <rdm at rcblue.com> wrote
>
> > return "%02d:%02d:%2.2f" % (hours, minutes, seconds)
>
> > print secsToHMS(4.04987) --> 00:00:4.05 (I wanted 00:00:04.05)
> >
> > I don't see how to get 00:00:04.05 using %f .
> > <http://www.python.org/doc/2.4.1/lib/typesseq-strings.html> is as
> > clear as mud.
> >
>
>%f is like this:
>
><justification><minimum width>.<decimal places>f
>
>If you provide a - sign it left justifies, without a minus right
>justifies
>
>The minimum width includes the decimal point. So in your case
>you want 2 + 1 + 2 = 5
>
>decimal places is number of digits after the point
>
>So you need:
>
>%5.2f
Well, I'm getting closer, but still no cigar:
def secsToHMS(seconds):
"""
Convert seconds to hours:minutes:seconds, with seconds rounded
to hundredths of a second
"""
#hours, minutes = 0, 0
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
return "%02d:%02d:%5.2f" % (hours, minutes, seconds)
print secsToHMS(4.04987)
This prints 00:00: 4.05 not 00:00:04.05
Dick
More information about the Tutor
mailing list