程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python3 tutorial: controlling the number of Python floating point number output bits

編輯:Python

stay Python In some long-term tasks , The inevitable need to send text files 、 Write some data to binary file or database , Or output some text on the screen , At this time, how to control the length of output data is a problem that needs our attention . For example, for a binary file , If the length of the output floating-point number is changing all the time , Then write to the file , If the reader reads according to the bits, he will read a pile of wrong data . therefore , We need to control the number of output bits , Especially for floating-point numbers, be extra careful .

Conventional control methods

In general , We can go through round To set the significant number of the output floating-point number , The principle is that for a given floating-point number, it is directly taken before n A significant number of bits , Subsequent figures are rounded off . and %.4f and {:.4f} These two formats , Is to take the decimal point after the output 4 Bit printing , It's different from taking significant numbers before printing , It will be mentioned later . Let's take a look at the output similarities and differences of these methods :

In [1]: pi_10=31.415926
In [2]: print (round(pi_10,4))
31.4159
In [3]: print ('%.4f' % pi_10)
31.4159
In [4]: print ('{:.4f}'.format(pi_10))
31.4159

The above example is that there are multiple digits before and after the decimal point , In addition, you can see that there is only... Before the decimal point 0 The digital output of :

In [5]: pi_10=0.31415926
In [6]: print (round(pi_10,4))
0.3142
In [7]: print ('{:.4f}'.format(pi_10))
0.3142
In [8]: print ('%.4f' % pi_10)
0.3142

In the above two cases , We find that the output results of the three are the same . But as mentioned earlier, take the significant number first and then output , It's different from outputting first and then taking significant numbers , It can be reflected through the following case :

In [9]: pi_10=3.1415926E-08
In [10]: print (round(pi_10,4))
0.0
In [11]: print ('{:.4f}'.format(pi_10))
0.0000
In [12]: print ('%.4f' % pi_10)
0.0000

The input here is a decimal point, there are many 0 Floating point number , But the scientific counting method is used here , That is to say 3.1415926∗10−8
, At this point, we use these three output modes , The results are all 0, And the first scheme is not full after the decimal point 4 position . This is because you are using round When taking significant numbers , There are too many numbers after the decimal point , Take the current floating-point number directly as 0.0 instead of 0.0000, The process of the latter two schemes is more like after printing this number , Then remove the number exceeding the significant digits , Therefore, the number after the decimal point will be retained 4 individual 0. however , Even so , The result is not what we want . Because although this number is very small , But maybe it's just a unit problem , It doesn't mean that the impact of this number is 0, Therefore, directly using this method to take the significant number after the decimal point may have problems .

Take significant numbers

Here we want to introduce the method of taking significant numbers , It is no longer a significant number after the decimal point , It's the overall significant number . And it's very simple , Is to put the... In the previous chapter {:.4f} Change to {:.4}, Similarly, the results of controlling significant figures can be understood through these cases :

''' No one answers the problems encountered in learning ? Xiaobian created a Python Exchange of learning QQ Group :153708845 Looking for small partners who share the same aspiration , Help each other , There are also good video tutorials and PDF e-book ! '''
In [13]: pi_10=3.1415926E-08
In [14]: print ('{:.4}'.format(pi_10))
3.142e-08
In [15]: pi_10=0.31415926
In [16]: print ('{:.4}'.format(pi_10))
0.3142
In [17]: pi_10=31.415926
In [18]: print ('{:.4}'.format(pi_10))
31.42

We found that , The output result will change according to the input format , If you enter decimal places 0 Too much , The output will be automatically converted into scientific counting method . Not just for floating point numbers ,{:.4} It can also act on strings , The effect is as follows :

In [19]: string='Hello World!'
In [20]: print ('{:.4}'.format(string))
Hell

summary

stay python In the output of , Especially the output of floating-point numbers , When we need to write a text file , It is best to use a unified output format , This can also enhance the readability of the results . For the control of floating-point number output bits , Can pass {:.4f}、%.4f To specify the space occupied by the string when printing or outputting , It can also be done through round Function to convert the results before output . And if it's a significant number , Need to use {:.4}, These methods have no advantages or disadvantages , Only look at different scenes , Choose different precision control schemes .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved