如何在Python随机数中限制小数点后的数字位数

2024-09-27 00:14:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用此代码:newAgent = [random.gauss(0, 1), random.gauss(0, 1)] 我想限制小数后的位数点。现在大约是20位数。在


Tags: 代码random小数位数gaussnewagent
1条回答
网友
1楼 · 发布于 2024-09-27 00:14:05

答案很简单。只需使用round,然后用, #Number of spaces指定位置。在这个例子中,我做了5个小数位。在

import random
newAgent = [random.gauss(0, 1), random.gauss(0, 1)]
print(round(newAgent[0], 5), round(newAgent[1], 5))

相关问题 更多 >

    热门问题