In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Most people do not understand the knowledge points of this article "how to control Python floating point output digits", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to control Python floating point output digits" article.
In the output of python, especially the floating-point output, when we need to write to a text file, it is best to use a unified output format, which can also enhance the readability of the results. For the control of floating-point output digits, you can specify that the string occupies space when printing or output through {: .4f},% .4f, or you can convert the results before output through the round function. If you take the effective number, you need to use {:. 4}, these methods have no advantages and disadvantages, only look at different scenes and choose different precision control schemes.
Technical background
In some long-term tasks of Python, it is inevitable to write some data to the text file, binary file or database, or output some text on the screen. At this time, how to control the length of the output data is a problem we need to pay attention to. For example, for a binary file, if the length of the output floating-point number changes all the time, after writing to the file, the reader reads according to the bits and reads a pile of incorrect data. Therefore, we need to control the number of output digits, especially floating-point numbers with extra care.
Conventional control method
In general, we can set the significant number of the output floating-point number through round, the principle is that for a given floating-point number directly take the first n significant digits, the subsequent numbers are rounded. The% .4f and {: .4f} formats print 4 decimal places after the output, which is not the same as taking a valid number first and then printing, which will be mentioned later. Let's take a look at the output similarities and differences of these ways:
In [1]: pi_10=31.415926In [2]: print (round (pi_10,4)) 31.4159In [3]: print ('.4f'% pi_10) 31.4159In [4]: print ('{: .4f} '.format (pi_10)) 31.4159
The above example shows that there are multiple digits before and after the decimal point, and similarly, you can take a look at the output of a number with only 0 before the decimal point:
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 found that the output results of the three are all the same. However, the difference between taking a valid number before output and then taking a valid number can be reflected in the following case:
In [9]: pi_10=3.1415926E-08In [10]: print (round (pi_10,4)) 0.0In [11]: print ('{: .4f} '.format (pi_10)) 0.0000In [12]: print (' .4f'% pi_10) 0.0000
The input here is a floating point number with a lot of zeros after the decimal point, but here we use the scientific counting method, that is,\ (3.1415926 * 10 ^ {- 8}\). In this case, we use these three output methods, and the results are all zero. and the first scheme is directly less than 4 digits after the decimal point. This is because when using round to take valid numbers, we find that there are too many numbers after the decimal point, and the current floating point number is directly regarded as 0.0000 rather than 0.0, while the process of the latter two schemes is more like printing out this number, and then removing the numbers that exceed the significant digits, so the 4 zeros after the decimal point will be retained. But even so, the result is not what we want. Because although this number is very small, it may only be due to a unit problem, which does not mean that the impact of this number is zero, so it may be problematic to directly use this method of taking a valid number after the decimal point.
Take a significant number
The method we are going to introduce here is no longer the effective number after the decimal point, but the whole effective number. The method is also very simple, which is to change {: .4F} in the previous chapter to {: .4}. Similarly, the results of controlling valid numbers can be understood through the following cases:
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 find that the result of the output will change according to the format of the input, and if the input has too many decimal places, the output will be automatically converted to scientific counting. Not just for floating-point numbers, {:. 4} can also be used in a string, as follows:
In [19]: string='Hello Worldwide'
In [20]: print ('{: .4} '.format (string))
Hell
The above is about the content of this article on "how to control Python floating-point output digits". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.