What is the Python round method?
Round is an inbuilt Python function that returns floating-point numbers rounded to a specified number of decimal places.
The Python round function takes two arguments, the first argument is the number that is going to be rounded off while the second argument is the decimal places that we want the number rounded off to.
Basic use of the Python Round method
However, it is not mandatory that we supply two arguments to the Python round function. It is important that we note that if we supply the number as the only argument we will get the nearest number whole integer.
In the case above we are getting 15 because 15 is the nearest whole integer. Suppose that we change the number to 15.6, then in this case we are going to get 16.
Therefore, no matter how many numbers we put at the end of the float integer, if we do not supply the second argument into the Python round function it is always going to return the nearest whole integer. As illustrated in the code below.
Using decimals with Python Round method
So in order to supply the second parameter, we’ve got to put a comma just after the number that we are rounding off and then supply the second argument which is the number of decimal places that we want the number rounded off to.
We can start off by rounding off the above number to two decimal places by supplying two as the second argument.
It is important to note that this does not return the number that we are expecting. Therefore if we change the number that we are rounding off to 2.675 we would expect that we should get 2.8.
Example 2
However, in this specific instance, we get 2.7 as the result. This is because most decimal fractions can’t actually be represented as floating-point numbers.
We should note that we are using the word around and not round up or round down. This is because there is no option of either specifically rounding up or rounding down; we’re only able to round off to the nearest value.
Example 3
However, in cases where a number is equidistant, the Python round function prefers to return the nearest even number.
This can be illustrated in cases such as when we try to round off 0.5 and -0.5. In both of these cases, we get Zero since this is the closest even number.
If we also try to round off -0.5 we are also going to get 0 since this is the closest even number as well.
Summary
If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.
In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.
Programming tutorials
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.
Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING
Not sure if programming is for you? With CodeBerry you’ll like it.
