python - 在 numpy jupyter notebook 中打印浮点精度
问题描述
我想打印精度为 4 的浮点数。我使用 numpy ,我试过的 jupyter 笔记本:
%precision %.4g
%precision 2
np.set_printoptions(precision=2)
print(0.6776776)
输出:
0.6776776
任何想法有什么问题?
# Name Version
ipython 6.4.0
ipython_genutils 0.2.0
msgpack-python 0.5.6
python 3.6.5
python-dateutil 2.7.3
解决方案
Print 不关心 numpy 或 IPython 格式化程序。它在后台调用 str(),任何格式化都必须使用标准打印格式化程序(%.2f 等)完成。查看不同的输出:
%precision 3
a = 0.6776776
print(a)
a
结果将是:
0.6776776
0.678
推荐阅读
- flutter - Flutter,image_cropper,如何以特定比例调整大小
- imagemagick - 如何使用 Node GraphicsMagick/ImageMagick 获取 gif 帧
- linux - 如何将儒略日期转换为公历日期并使用 Shell 脚本从中减去几天
- python - 如何在 Python 中直接在 s3 中的文件中写入 JSON?
- python - 在使用客户端 Boto3 SDK 时获取不带父文件夹前缀的确切文件名
- python - 如何修复 RuntimeWarning:协程
- ibm-midrange - DSPUSRPRF UPCHGD 字段
- python - 关于根据Flask_restx的API对象声明位置路由函数的问题
- ethereum - 我们有两种方式来启动一个 web3 实例,我们选择哪一种更好呢?
- python - 如何在 Python 3.8 的 dbf 文件中写入/附加列?