Python

How to Round Numbers in Python

Hello in this tutorial, we will understand the round() method to round the numbers while working with decimal numbers in python programming.

1. Introduction

The round() method in python is used to round the numbers while working with decimal numbers. It is represented with the following syntax –

1
round(number, digits)

Where,

  • number is a required param for the number to be rounded
  • digits is an optional param for the number of decimals to use when rounding the number
    • default is 0
    • if the last digit after the decimal is >=5 it will be round off to the next integer
    • if the last digit after the decimal is <5 it will be round off to the floor integer

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. How to Round Numbers in Python

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice.

2.1 When the decimal parameter is missing

Let us understand this with the help of a code snippet.

When the decimal parameter is missing

1
2
3
4
5
6
7
8
9
def method1():
    print(round(10))
    print(round(10.2))
    print(round(10.6))
def main():
    print('==== if the decimal param is not present =====')
    method1()
if __name__ == '__main__':
    main()

If everything goes well the following output will be shown in the IDE console.

Console Output

1
2
3
4
==== if the decimal param is not present =====
10
10
11

2.2 When the decimal parameter is present

Let us understand this with the help of a code snippet.

When the decimal parameter is present

01
02
03
04
05
06
07
08
09
10
11
def method2():
    print(round(10.465, 2))
    # if the last digit after the decimal is >=5, it will be round off to the next integer
    print(round(10.466, 2))
    # if the last digit after the decimal is <5, it will be round off to the floor integer
    print(round(10.463, 2))
def main():
    print('\n==== if the decimal param is present =====')
    method2()
if __name__ == '__main__':
    main()

If everything goes well the following output will be shown in the IDE console.

Console Output

1
2
3
4
==== if the decimal param is present =====
10.46
10.47
10.46

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned –

  • An introduction to round() method in python programming
  • Sample program to understand the round() method

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was an example of implementing the round() method in python programming.

Download
You can download the full source code of this example here: How to Round Numbers in Python

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button