[英]How to set a general option in Python to display only N digits everywhere?
I want to work with 3 digits after the decimal point in Python. 我想在Python中使用小数点后的3位数字。 What is the relevant setting to modify ?
需要修改哪些相关设置?
I want that 1.0 / 3
would return 0.333, and not 0.3333333333333333 like it is the case in my Jupyter Notebook, using python 2.7.11 and Anaconda 4.0.0. 我希望
1.0 / 3
返回0.333,而不是0.3333333333333333,就像使用Python 2.7.11和Anaconda 4.0.0的Jupyter Notebook一样。
In my research, I heard about the Decimal class, but I don't want to use Decimal(x) in my code every time I display a float, neither the string formating or the round function, though I use it for the time being (because I don't want to use it every time). 在我的研究中,我听说了Decimal类,但是我不想每次显示浮点数时都在代码中使用Decimal(x),尽管我暂时不使用它格式化字符串或舍入函数。 (因为我不想每次都使用它)。
I think there is a general solution, a setting computed only once. 我认为有一个通用的解决方案,一个设置只计算一次。
use numpy and try this; 使用numpy尝试一下;
round(1.0/3, 3)
or 要么
>>> 1.0/3
0.3333333333333333
>>> '{:0.3f}'.format(1.0/3)
'0.333'
There is no "one-time" solution to your problem. 没有针对您的问题的“一次性”解决方案。 And I think that your approach might be a little misguided.
而且我认为您的方法可能会被误导。
I suppose that your interaction with Jupyter or Ipython has lead you to the conclusion that python is quite handy as a numerical calculator. 我想您与Jupyter或Ipython的互动已经使您得出结论,python作为数字计算器非常方便。 Unfortunately both of the aforementioned programs are just wrappers or REPL programs and in the background come with the full programming language flexibility that Python offers.
不幸的是,上述两个程序都是包装程序或REPL程序,并且在后台提供了Python提供的完整编程语言的灵活性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.