首页 > 解决方案 > 在 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       

标签: pythonnumpyjupyter-notebookprecision

解决方案


Print 不关心 numpy 或 IPython 格式化程序。它在后台调用 str(),任何格式化都必须使用标准打印格式化程序(%.2f 等)完成。查看不同的输出:

%precision 3
a = 0.6776776
print(a)
a

结果将是:

0.6776776
0.678

推荐阅读