设置ipython的默认科学标记阈值

时间:2023-01-14 16:19:15

How can I modify the point at which python decides to print in scientific notation?

我如何修改python决定用科学符号打印的点?

E.g. I'd like everything > 1e4 or < 1e-4 to be printed in scientific notation.

我希望> 1e4或< 1e-4都用科学符号打印。

Good:

好:

In [11]: 5e20
Out[11]: 5e+20

Bad:

缺点:

In [12]: 5e10
Out[12]: 50000000000.0

2 个解决方案

#1


11  

In IPython you can use

在IPython中,您可以使用

%precision %.4g

this will print floating point values who's absolute value is < 1e-4 or >= 1e4 in scientific notation.

这将打印浮点值,其绝对值在科学表示法中为< 1e-4或>= 1e4。

You can find more information about the %precision command in the IPython API Docs. For string formatting options have a look at the Python Docs

您可以在IPython API文档中找到关于%precision命令的更多信息。对于字符串格式化选项,请查看Python文档

#2


0  

Actually it has nothing to do with IPython as it simply calls __str__. Sadly, float, being a built-in type, is implemented in C and you can't monkeypatch C types. So your only options are modifying __str__ implementation in cpython or patching IPython to be smarter than just calling __str__.

实际上它与IPython没有任何关系,因为它只调用__str__。遗憾的是,float是一个内置类型,它是在C中实现的,您不能使用monkeypatch C类型。因此,您唯一的选择是在cpython中修改__str__实现或修补IPython,使其比仅仅调用__str__更智能。

#1


11  

In IPython you can use

在IPython中,您可以使用

%precision %.4g

this will print floating point values who's absolute value is < 1e-4 or >= 1e4 in scientific notation.

这将打印浮点值,其绝对值在科学表示法中为< 1e-4或>= 1e4。

You can find more information about the %precision command in the IPython API Docs. For string formatting options have a look at the Python Docs

您可以在IPython API文档中找到关于%precision命令的更多信息。对于字符串格式化选项,请查看Python文档

#2


0  

Actually it has nothing to do with IPython as it simply calls __str__. Sadly, float, being a built-in type, is implemented in C and you can't monkeypatch C types. So your only options are modifying __str__ implementation in cpython or patching IPython to be smarter than just calling __str__.

实际上它与IPython没有任何关系,因为它只调用__str__。遗憾的是,float是一个内置类型,它是在C中实现的,您不能使用monkeypatch C类型。因此,您唯一的选择是在cpython中修改__str__实现或修补IPython,使其比仅仅调用__str__更智能。