Python Forum
Converting a remainder to int to str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Converting a remainder to int to str
#3
I think that one should start from another end by dividing seconds with 60 (to get seconds which don't fit into minutes), then dividing floor with 60 (to get minutes which don't fit into hours) and then divide with 60 to get hours. There is built-in divmod() for this type of calculations:

EDIT: following is code which doesn't deliver expected results but kept for context. Refer to second code snippet

>>> seconds = 50007
>>> timeunits = list()
>>> for i in range(3):                      # for sec, min and hrs
...    seconds, unit = divmod(seconds, 60)  
...    timeunits.insert(0, unit)
...
>>> timeunits
[13, 53, 27]
>>> ':'.join(str(unit) for unit in timeunits)
'13:53:27'
Second snippet delivering expected results:

>>> seconds = 500007
>>> minutes, seconds = divmod(seconds, 60)
>>> hours, minutes = divmod(minutes, 60)
>>> print(f'{hours}:{minutes}:{seconds}')
138:53:27
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Converting a remainder to int to str - by tester_V - Oct-27-2020, 07:13 PM
RE: Converting a remainder to int to str - by perfringo - Oct-27-2020, 09:03 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Could not parse the remainder: '=' from '=' Saurabh 3 14,511 Feb-20-2019, 11:18 AM
Last Post: buran
  remainder operator % noweare 3 2,836 Feb-14-2019, 01:50 AM
Last Post: noweare

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020