set ipython's default scientific notation thre

2019-02-12 14:53发布

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

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

Good:

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

Bad:

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

2条回答
老娘就宠你
2楼-- · 2019-02-12 15:07

In IPython you can use

%precision %.4g

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

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

查看更多
成全新的幸福
3楼-- · 2019-02-12 15:25

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__.

查看更多
登录 后发表回答