首页 > 解决方案 > np.float64 与原生 Python 浮点的 str 表示的差异

问题描述

在本机 Python类型中,当被强制转换为 afloat时,似乎没有四舍五入:floatstr

In [1]: foo = 0.24000000000000002

In [2]: print(str(foo))
0.24000000000000002

In [3]: print(repr(foo))
0.24000000000000002

但是,当np.float64被转换为 a时str,似乎会发生舍入。

In [4]: print(str(np.float64(foo)))
0.24

In [5]: print(repr(np.float64(foo)))
0.24000000000000002

np.float64被转换为 a时,舍入背后的逻辑是str什么?

标签: pythonnumpy

解决方案


推荐阅读