Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:时间模块的舍入输出_Python - Fatal编程技术网

Python:时间模块的舍入输出

Python:时间模块的舍入输出,python,Python,这是我用来测量用户完成程序所用时间的代码: 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 我的问题是,我该如何将输出四舍五入到小数点后一位?例如,如果我的输出是3.859639秒我将如何表示:3.8秒?看起来您忘记了“f”之前的“.1”。试试这个: print("%.1f

这是我用来测量用户完成程序所用时间的代码:

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

我的问题是,我该如何将输出四舍五入到小数点后一位?例如,如果我的输出是
3.859639秒
我将如何表示:
3.8秒?

看起来您忘记了“f”之前的“.1”。试试这个:

print("%.1f seconds" % (time.time() - start_time))

看起来你忘记了“f”之前的“.1”。试试这个:

print("%.1f seconds" % (time.time() - start_time))

原始方式。。。相乘,截断其余部分,然后再次除法:

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

原始方式。。。相乘,截断其余部分,然后再次除法:

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

使用第二个参数为1的round函数

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

使用第二个参数为1的round函数

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

哎哟,打错了。我的意思是说3.8秒你确定不应该是3.9秒吗?那么,你想一直四舍五入吗?而不是四舍五入3.859。。。到3.9?如果需要舍入值,请查看
round()
函数:。要进行四舍五入,您可以减去0.5,然后
round()
Ewh,这就是输入错误。我的意思是说3.8秒你确定不应该是3.9秒吗?那么,你想一直四舍五入吗?而不是四舍五入3.859。。。到3.9?如果需要舍入值,请查看
round()
函数:。要进行四舍五入,你可以减去0.5,然后
round()
这对我很有效,用3.9代替3.8不会杀死我的程序,所以这很好。这对我很有效,用3.9代替3.8不会杀死我的程序,所以这很好。