The round function is very simple. It approximates the floating-point number and keeps a few decimal places. such as
round(10.0/3,2)3.33round(20/7)3
The first parameter is a floating point number, and the second parameter is the reserved decimal places, optional, if not written, it will be reserved as an integer by default.
What pits can there be with such a simple function?
1、 The result of round is related to the python version
Let's see what is the difference between python2 and python3:
$ python
Python 2.7.8(default, Jun 182015,18:54:19)[GCC 4.9.1] on linux2
Type "help","copyright","credits" or "license"for more information.round(0.5)1.0
$ python3
Python 3.4.3(default, Oct 142015,20:28:29)[GCC 4.8.4] on linux
Type "help","copyright","credits" or "license"for more information.round(0.5)
If we read the python documentation, it is written like this:
In the python2.7 doc, the end of round() says, "Values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done away from 0." The reserved value will Keep to the end closer to the previous one (rounded to six). If the distance is the same as the two ends, keep to the side farther from 0. So round(0.5) will approximate to 1, and round(-0.5) will approximate to -1.
But in the python3.5 doc, the document becomes "values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice." , Will be reserved to the even side. For example, round(0.5) and round(-0.5) will be retained to 0, and round(1.5) will be retained to 2.
So if there is a project that is migrated from py2 to py3, pay attention to the round (of course, also pay attention to / and //, as well as print, and some more alternative libraries).
**2、 The result of special number round may not be what you want. **
round(2.675,2)2.67
The doc of python2 and python3 both cite the same chestnut. The original text says this:
Note
The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected
2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a
float. See Floating Point Arithmetic: Issues and Limitations for more information.
Simply put, the result of round(2.675, 2), no matter if we look at it from python2 or 3, the result should be 2.68, and it turns out to be 2.67. Why? This is related to the precision of floating point numbers. We know that floating-point numbers in the machine may not be accurately expressed, because it may be an infinite number of digits after being converted into a string of 1 and 0, and the machine has already made truncation processing. Then the number 2.675 stored in the machine is slightly smaller than the actual number. This little bit causes it to be a little closer to 2.67, so it approximates to 2.67 when two decimal places are kept.
the above. Unless there is no requirement for accuracy, try to avoid using the round() function. We have other options for approximate calculations:
Use some functions in the math module, such as math.ceiling (ceiling division).
Python comes with its own divisible, python2 is /, 3 is //, and there is also a div function.
String formatting can be truncated, such as "%.2f"% value (retain two decimal places and turn it into a string... If you want to use floating point numbers, please put on float()).
Of course, if the precision requirements for floating-point numbers are very high, please use 嘚瑟馍, right? Please use the decimal module.
Content expansion:
round(number,num_digits)
Number The number to be rounded.
The number of digits specified by Num_digits will be rounded off according to this number.
annotation
Example
x=1.343671234
print x
print round(x,1)
print round(x,2)
print round(x,3)
The output is:
1.343671234
1.3
1.34
1.344
So far, this article on how to use the round function in python is introduced. For more relevant Python round function usage summary content, please search for ZaLou.Cn's previous articles or continue to browse the related articles below. Hope you will support ZaLou more in the future. .Cn!
Recommended Posts