0

how do I keep the output to two decimal places

no decimal places:

 import time
 print ("60 SECOND TIMER")
 run = input('click ENTER to run')
 secs=0
 while secs < 60:
      print(60 - secs)
      time.sleep(1)
      secs = secs+1

two decimal places:

 import time
 print ("60 SECOND TIMER")
 run = input('click ENTER to run')
 secs=0
 while secs < 60:
      print(60 - secs)
      time.sleep(0.01)
      secs = secs+0.01

Quick note: two decimal places starts going mad (ends up with 8 or 9 decimal places

  • I see that Python has a [round() function](https://docs.python.org/3/library/functions.html#round). Does that help? – Andrew Morton Feb 07 '15 at 19:58
  • Was that a comment to me or an addition to your question? If the latter, then please edit your question to include that information. – Andrew Morton Feb 07 '15 at 20:01
  • thanks for the info, andrew. could you insert the round function, as i tried and it did nothing (I fixed the other comment) – Fox Lyefield Feb 07 '15 at 20:06
  • You should again edit your question to add on how you've tried using `round`, e.g. "I tried `print(round(60 - secs, 2)` and it still showed more than two decimal places." P.S. If you start a comment with "@AndrewMorton" then I will be notified. – Andrew Morton Feb 07 '15 at 20:14
  • ...oops... `print(round(60 - secs, 2))` – Andrew Morton Feb 07 '15 at 20:25
  • If that solved it for you, please let us know and I'll make my suggestion into an answer for you. – Andrew Morton Feb 07 '15 at 20:30
  • @AndrewMorton It works, but the sleep command needs to be 10x shorter to count accurately – Fox Lyefield Feb 08 '15 at 07:11
  • @AndrewMorton scratch that, runs in command prompt without needing to be shorter. plz publish your method as an answer – Fox Lyefield Feb 19 '15 at 13:54

2 Answers2

0

try time.sleep(.01 - timer() % .01), to lock the sleep with the timer(). Though it won't help if either time.sleep() or timer() do not support 10ms granularity. It may also depend on how Python interpreter switches between threads (GIL acquire/release) and OS scheduler (how busy the system is and how fast OS can switch between processes/threads).

To pause for a short duration, you could try a busy loop instead:

from time import monotonic as timer

deadline = timer() + .01
while timer() < deadline:
    pass

For example, to do something every 10ms for a minute using time.sleep() would probably fail:

import time
from time import monotonic as timer

now = timer()
deadline = now + 60 # a minute
while now < deadline: # do something until the deadline
     time.sleep(.01 - timer() % .01) # sleep until 10ms boundary
     now = timer()
     print("%.06f" % (deadline - now,))

but the solution based on a busy loop should be more precise:

import time
from time import monotonic as timer

dt = .01 # 10ms
time.sleep(dt - timer() % dt)  
deadline = now = timer()    
outer_deadline = now + 60 # a minute
while now < outer_deadline: # do something until the deadline
     print("%.06f" % (outer_deadline - now,))

     # pause until the next 10ms boundary
     deadline += dt
     while now < deadline:
         now = timer()
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

Use the round() function like this:

print(round(60 - secs, 2))

to output the remaining time to two decimal places.

Incidentally, printing it every 10 ms may be a bit optimistic considering that your display is probably only updated 60 times a second, i.e. at 16.67 ms intervals.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
  • `secs` is misleading here. `round()` may help to hide the floating-point error due to `+=.01` but it can't help to sync. `secs` with the actual time. `time.sleep(0.01)` may sleep both less and more than 10ms. After several iterations `secs += .01` won't reflect the actual time even if the computation were exact. – jfs Feb 20 '15 at 02:45