为什么我需要time()函数的round()函数?

nic*_*day 3 python

以下是我经常看到的代码示例:

import time
def gettime():
    return int(round(time.time()*1000))
Run Code Online (Sandbox Code Playgroud)

在这种情况下,round()在此代码中使用的原因是什么?time.time()总是在小数点后返回1-2位数,所以round()函数似乎没用.

Mar*_*oun 6

这是不是无用的,time.time()在我的系统会返回类似:

1430407063.751232
Run Code Online (Sandbox Code Playgroud)

乘以1000返回:

1430407063751.232
Run Code Online (Sandbox Code Playgroud)

round对此进行舍入1430407063751.0,但如果是 1430407063751.532,则将其舍入到1430407063752.0.

time.time():

返回自纪元以来的秒数作为浮点数.请注意,即使时间总是作为浮点数返回,但并非所有系统都提供的精度高于1秒.虽然此函数通常返回非递减值,但如果在两次调用之间设置了系统时钟,则它可以返回比先前调用更低的值.