gpt4 book ai didi

python - Python 中的精度舍入

转载 作者:行者123 更新时间:2023-12-01 00:12:57 25 4
gpt4 key购买 nike

如果某些Python代码的输出格式类似于“x.xx”如何提高其精度?

例如:

import math

tc= int(input())
for i in range(0,tc):
x1,y1,x2,y2,v1,v2= map(int,input().split())

#distance
# d = abs(math.sqrt( abs((x1-x2)*(x1-x2)) + abs((y1-y2)*(y1-y2)) ))

#d1
d1 = abs(math.sqrt( abs((x1-x1)*(x1-x2)) + abs((y1-0)*(y1-0)) ))


#d2
d2 = abs(math.sqrt( abs((x1-x2)*(x1-x2)) + abs((0-y2)*(0-y2)) ))


niche = d1/v1
upar = d2/v2

roundoff = float("{:.5f}".format(niche+upar))
print(roundoff)

它的输出是5.5使其输出为 5.50000。

最佳答案

print(roundoff) 可以很好地打印浮点值。所以你只需要删除浮点转换:

roundoffstr = "{:.5f}".format(niche+upar)
print(roundoffstr)

关于python - Python 中的精度舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59505170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com