How to properly round numpy float arrays

*爱你&永不变心* 提交于 2019-12-06 09:22:06

问题


I have some sort of rounding issue when rounding floats.

x = np.array([[1.234793487329877,2.37432987432],[1.348732847,8437.328737874]])
np.round(x,2)

array([[  1.23000000e+00,   2.37000000e+00],
       [  1.35000000e+00,   8.43733000e+03]])

Is there a way to display these numbers without the zero extensions?


回答1:


Rounding floating point numbers is almost never needed (unless you want to bucket them, then your code will work just fine), if you only want to print them with less precision, use this:

print(np.array_str(x, precision=2))


来源:https://stackoverflow.com/questions/40288406/how-to-properly-round-numpy-float-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!