如何正确舍入 numpy 浮点数组 [英] How to properly round numpy float arrays

查看:59
本文介绍了如何正确舍入 numpy 浮点数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在舍入浮点数时我有一些舍入问题.

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?

推荐答案

四舍五入浮点数几乎是不需要的(除非你想把它们存储起来,那么你的代码会工作得很好),如果你只想用精度较低,使用此:

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))

这篇关于如何正确舍入 numpy 浮点数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆