user3093377
user3093377

Reputation: 41

Issue with converting seconds to hh:mm:ss with large numbers in Python

The input I get the user to enter is seconds. Most of the time I am able to convert my input to the hh:mm:ss format but when the numbers get a bit larger I seem to run into some issues. For example when I input 900, I get a result of 01:15:-100. I especially don't want negatives and only want two digests (no decimals either) per each number. Not sure if I have gone wrong on my math.

if sys.argv[1] == "raceTime":
n = int(sys.argv[2])
if n < 60:
    s = format(number, '0>2.0f')
    print("{}:{}:{}".format(00, 00, s))
else:
    n = int (sys.argv[2])/60
    m =n//1
    h = min//10
    s = (n - h - m)*100
    second = format(s,'0>2.0f')
    minute = format(m, '0>2.0f')
    hour = format(h, '0>2.0f')
    print("{}:{}:{}".format(hour, minute, second))

Upvotes: 1

Views: 373

Answers (1)

user2390182
user2390182

Reputation: 73470

Use proper string formatting:

second = format(sec, '02.0f')  # etc.

or just

print("{:02.0f}:{:02.0f}:{:02.0f}".format(hour, min, sec))
# f -> float, 02 -> 2 places before (0-padded), .0 -> 0 places after point

Upvotes: 2

Related Questions