converting an int to a string

S

Sean Farrow

Hi:
I have the folling code:
def parseTime(self, time):
minutes =int(float(time)/60)
seconds =int(float(time)-minutes*60)
minutes =str(minutes)
seconds =str(minutes)
the statements that convert the minutes and seconds variables
str(minutes) and str(seconds) don't seem to be working.
Any idea why?
is there any other way of doing this and perhaps using the $d
interpolation operator?
sean.
 
D

Diez B. Roggisch

Sean said:
Hi:
I have the folling code:
def parseTime(self, time):
minutes =int(float(time)/60)
seconds =int(float(time)-minutes*60)
minutes =str(minutes)
seconds =str(minutes)
the statements that convert the minutes and seconds variables
str(minutes) and str(seconds) don't seem to be working.
Any idea why?
is there any other way of doing this and perhaps using the $d
interpolation operator?

What do you mean by "don't seem to be working"

Usually, they do:

Python 2.4.4 (#1, Oct 18 2006, 10:34:39)
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Welcome to rlcompleter2 0.96

Any change you overdefined str with something weird?

Diez
 
P

Paul McGuire

Hi:
I have the folling code:
def parseTime(self, time):
minutes =int(float(time)/60)
seconds =int(float(time)-minutes*60)
minutes =str(minutes)
seconds =str(minutes)
the statements that convert the minutes and seconds variables
str(minutes) and str(seconds) don't seem to be working.
Any idea why?
is there any other way of doing this and perhaps using the $d
interpolation operator?
sean.

The divmod builtin function is your friend here.

def parseTime(time):
seconds = float(time)
minutes,seconds = divmod(seconds,60)
return "%02d:%06.3f" % (minutes,seconds) # just guessing

divmod divides the first argument by the second, and returns the tuple
(quotient,remainder).

-- Paul
 
J

John Machin

Hi:
I have the folling code:
def parseTime(self, time):
minutes =int(float(time)/60)
seconds =int(float(time)-minutes*60)
minutes =str(minutes)
seconds =str(minutes)

seconds = str(minutes) ???
Is that the actual code that you executed?
What does "don't seem to be working" mean? The seconds output is
always the same as the minutes output?
Try again, with
seconds = str(seconds)
followed by
print "debugging", repr(time), repr(minutes), repr(seconds)
If it still seems not to be working, come back with a copy/paste of
the actual code that you executed, plus a copy/paste of the output
the statements that convert the minutes and seconds variables
str(minutes) and str(seconds) don't seem to be working.
Any idea why?
is there any other way of doing this and perhaps using the $d
interpolation operator?

What is the $d interpolation operator? Do you mean the %d string
formatting operator? If so, the answer depends on what you want to do
with the output ... e.g.
"%d:%02d" % (int_minutes, int_seconds)
might be what you want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top