Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need round() function with time()?

Tags:

python

Here is an example of code I often see:

import time
def gettime():
    return int(round(time.time()*1000))

In this case, what is the reason of using round() in this code? time.time() always returns 1-2 digits after the decimal, so round() function seems useless.

like image 586
ratojakuf Avatar asked Dec 15 '22 14:12

ratojakuf


1 Answers

It is not useless, time.time() in my system returns something like:

1430407063.751232

multiplying by 1000 returns:

1430407063751.232

round rounds this to 1430407063751.0, but if it was 1430407063751.532, it'll round it to 1430407063752.0.

time.time():

Return the time in seconds since the epoch as a floating point number. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

like image 94
Maroun Avatar answered Dec 17 '22 03:12

Maroun