- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Python has an inbuilt round() function to round off a number.

The round() method in Python takes two parameters −

The first one is the number to be rounded off.

The second one specifies the number of digits to which the number must be rounded off.

Here, the second parameter is optional.

If second parameter is not specified, then the round() method returns the integer using floor() and ceil().

It will look for the digits after the decimal.

If those are less than 5, it returns the floor() of the number passed.

Whereas if the digits after decimal are greater than 5, it returns the ceil() of the number passed.

If integer number is passed, then the same number is returned.

It will be more clear from the implementation below −

The number to be rounded off is passed to the round() function. The output of round() in this case will always be an integer value.

Let us understand with an example.

print(round(15)) print(round(15.2)) print(round(15.8)) print(round(15.128)) print(round(15.89))

15 15 16 15 16

The explanation of all the print statements −

Integer 15 is passed, and the same integer is returned, hence output 15.

Number 15.2 is passed, the digits after decimal are less than 5, hence floor of 15.2 is returned, hence output 15.

Number 15.8 is passed, the digits after decimal are greater than 5, hence ceil of 15.8 is returned, hence output 16.

Number 15.128 is passed, the digits after decimal are less than 500, hence floor of 15.128 is returned, hence output 15.

Number 15.89 is passed, the digits after decimal are greater than 50, hence ceil of 15.89 is returned, hence output 16.

**Note:** All the outputs are integer values.

The second parameter gives the number of digits to which the number must be rounded off.

print(round(15,2)) print(round(15.2789,3)) print(round(15.82,1)) print(round(15.128,2)) print(round(15.8902,2))

15 15.279 15.8 15.13 15.89

The working of round() function, in this case, is the same as we calculate round off of a number mathematically.

In the case of integer value, the same integer value is returned, else the floating number rounded off to the specified number of digits is returned.

The number is 15.2789 and the number of digits is 3. The 4th digit after decimal is greater than 5, hence the 3rd digit (the last specified digit) will be incremented by 1. Hence the output will be 15.279.

The number is 15.82 and the number of digits is 1. The 2 nd digit after decimal is less than 5, hence the 1st digit is not incremented by 1. Hence the output will be 15.8.

- Related Questions & Answers
- How to round off a floating number using Python?
- Calculating percentage in a MySQL query and round off the result
- How to round up a number in JavaScript?
- How do you round up a float number in Python?
- Java program to round a number
- Round a number to the nearest even number in C#
- How to round a number to n decimal places in Java
- How can I round down a number in JavaScript?
- How can I round a number to 1 decimal place in JavaScript?
- How to round down to 2 decimals a float using Python?
- Java Program to round number to fewer decimals
- How to round the decimal number to the nearest tenth in JavaScript?
- Golang program to turn off the k’th bit in a number.
- Python - Round number of places after the decimal for column values in a Pandas DataFrame
- Round number down to nearest power of 10 JavaScript

Advertisements