How to use the round function in python

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

How to use the round function in python
How to use the zip function in Python
How to use the format function in python
How to use SQLite in Python
How to use and and or in Python
How to run id function in python
How to use code running assistant in python
How to install the downloaded module in python
How to use python tuples
How to understand the introduction of packages in Python
How to use Python's filter function
How to use python's help function
How to use hanlp in ubuntu
How Python implements the mail function
How to wrap in python code
How to save the python program
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to view errors in python
How to write return in python
How Python implements the timer function
How to view the python module
How to understand variables in Python
How to clear variables in python
How to find the area of a circle in python
How to delete cache files in python
How to introduce third-party modules in Python
How to represent null values in python
How to save text files in python
How to use PYTHON to crawl news articles
How to write win programs in python
How to install third-party modules in Python
How to custom catch errors in python
How to write try statement in python
How to define private attributes in Python
How to add custom modules in Python
How to understand global variables in Python
How to view installed modules in python
Python novice learns to use the library
How to learn the Python time module
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
What is the function of adb in python
How to configure TensorFlow use environment in Ubuntu
Join function in Python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to simulate gravity in a Python game
An article to understand the yield in Python
How to use dpkg command in Ubuntu system
How to open the ubuntu system in win10
How to enter python through the command line
How to set code auto prompt in python
How to switch the hosts file using python
Teach you how to write games in python
How to delete files and directories in python
Introduction to the use of Hanlp in ubuntu