Introduction
In this tutorial, we will learn how to round the nearest integer in Python with some of the examples, and we will explore the syntax and usage of python round() function through example programs.
What is round() function?
The round() function is an built-in function in Python. It rounds off numbers up to the specified number of decimal points. The round() function can be any type of data, any data type can undergo rounding using this function. The data type can be determined using type() function.
Syntax
rounded_number = round(numbers, ndigits)
Here, numbers indicates the number to be rounded off and number must be specified.
ndigits indicates the number up to which the given number is rounded. By default, ndigits are considered as ZERO. ndigits is optional.
print(round(21.54325637)) print(type(round(7))) print(round(62.4582,2))
What round() will return?
Case 1: It returns the Nearest integer to the given number if ndigits is not given. In this case the type would be INT.
Case 2: It returns the number rounded off to ndigits digit if ndigits is given. In this case the type would be FLOAT.
print(round(8)) print(round(25.56325637)) print(type(round(7))) print(round(62.4582,2)) print(round(6,2))
- If input number is an integer then the return value is also an integer.
- If input value is float, it will return an integer value if ndigits is not given. If ndigits are given, then it will return a float value.
print(round(10)) print(round(11.4)) print(round(11.56)) print(round(-8/3)) print(round(-8/3,2)) print(round(2.66666,2)) print(round(-1.5))