Velocity Reviews > converting an int to a string

# converting an int to a string

Sean Farrow
Guest
Posts: n/a

 06-08-2007
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.

Diez B. Roggisch
Guest
Posts: n/a

 06-08-2007
Sean Farrow schrieb:
> 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
Welcome to rlcompleter2 0.96
for nice experiences hit <tab> multiple times
>>> str(120)

'120'
>>>

Any change you overdefined str with something weird?

Diez

Paul McGuire
Guest
Posts: n/a

 06-08-2007
On Jun 8, 10:40 am, Sean Farrow <(E-Mail Removed)> wrote:
> 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

John Machin
Guest
Posts: n/a

 06-08-2007
On Jun 9, 1:40 am, Sean Farrow <(E-Mail Removed)> wrote:
> 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.