python - 如何使用python获得具有所需精度的 float

标签 python

例如,我想从 e(2.71 来自 2.718281828...)中获取前 3 位数字的 float ,有什么函数可以使用吗?我用 getcontext().prec=1 尝试了 round()、math.floor() 和 Decimal(1).exp(),但没有一个能给我我想要的(round() 给了我 2.72,math.floor () 2 和 Decimal(1).exp 也是 2.72) 我不想将 float 四舍五入。

提前致谢。

最佳答案

round(x - 0.005, 2)
# => 2.71
math.floor(x * 100) / 100
# => 2.71

关于python - 如何使用python获得具有所需精度的 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33249426/

相关文章:

python - 根据第一个数组的条件查找第二个数组中的值。最有效的方法是什么?

python - 如何实现多类语义分割?

python - 为什么 python 要求你在等待条件之前获取锁

python - raw_input 不在 readline 中留下历史记录

python - 'tf.get_default_session()'的用途

python - 无法使用selenium从页面中名为 'heading'的每个类获取数据

Python - 在 mplot3D 中设置 zlim

Python递归拆分路径

python - 以 nohup 启动的进程未与父进程分离

python - 如何在 Python 中集成 HTML