0

This is the code I have for measuring how long it took the user to complete the program:

start_time = time.time() # Sits at the top of my code print("%f seconds" % (time.time() - start_time)) # Goes at the bottom of my code 

My question is, how do I round the output of this to one decimal place? For example, if my output was 3.859639 seconds how would I present this like: 3.8 Secounds?

4
  • Ewh, that mistype. I meant to say 3.8 secounds Commented Oct 12, 2015 at 15:31
  • Are you sure it shouldn't be 3.9 seconds? Commented Oct 12, 2015 at 15:31
  • So, you want to round down all the time? Instead of rounding 3.859... to 3.9? Commented Oct 12, 2015 at 15:31
  • If you need the value rounded, the look into the round() function: docs.python.org/2/library/functions.html#round. To round down you could subtract 0.5 and then round() Commented Oct 12, 2015 at 15:31

3 Answers 3

5

It looks like you've forgotten ".1" before "f". Try this:

print("%.1f seconds" % (time.time() - start_time)) 
Sign up to request clarification or add additional context in comments.

1 Comment

This works for me, having 3.9 instead of 3.8 won't kill my program so it's fine.
0

Use round function with second argument as 1

start_time = time.time() # Sits at the top of my code x=round((time.time() - start_time),1) print x 

Comments

0

Primitive way... multiply out, truncate the rest, then divide again:

diff = time.time() - start_time rounded_down = int(diff * 10) / 10 # = 3.8 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.