Python round() Function

Python is a very versatile high-level programming language that is most widely used in Data Sciences, Machine Learning, and Artificial Intelligence. Python provides great support through built-in modules and functions where we need to play with the numbers. The Python round() function rounds off the floating-point number to the stated number of decimals and returns it.For example, we have a floating-point number 6.677, and we need to round it off to the 2 decimal points, then the round() function will do the job and round off the number to 6.68.

This article explains the Python round() function in detail with examples.

Syntax of round() function

The syntax of the round() function is as follows:

round(floating-point number, digits)

The round() function two parameters as arguments, i.e., floating-point number and digits. The number or floating-point number is the required parameter, whereas the number of digits is the optional parameter. In case if we do not provide the number of digits, then the round() function will return the closest integer number. We can also provide the integer number in the first parameter. In this case, the round() function will return the same integer number.

Examples and usage of round() function

Let’s see the examples and usage of the round() function in our Python script. If we do not specify the number of digits, then the round() function takes the ceil of the number and convert it into the next integer if the decimal value is greater than 5. In case if the decimal value is less than equal to the 5, then it takes the floor value, and the integer number remains the same.

#a program to round off the floating-point numbers

#not specifying the number of digits

print(round(10.1))

print(round(10.5))

print(round(10.7))

print(round(11.9))

print(round(15.3))

print(round(17.8))

print(round(20))

print(round(20.01))

Output

Now, let’s define the number of digits and use the round() function.

#a program to round off the floating-point numbers

print(round(10.123,2))

print(round(10.587,1))

print(round(10.72,1))

print(round(11.9545,1))

print(round(15.322,2))

print(round(17.865,2))

print(round(20.090,2))

print(round(20.01114,2))

Output

Now, let’s take some integer values and apply the round() function. You can note that in the output, then unchanged integer value is returned.

#a program to round off the floating-point numbers

print(round(10))

print(round(20))

print(round(30))

print(round(40))

print(round(50))

print(round(12))

print(round(15))

print(round(19))

Output

If we pass any string or character to the round() function instead of a number, the Python interpreter will throw an error.

#passing a string to round function

print(round('kamran'))

Output

Rounding off the negative numbers
The round() function can be applied to negative numbers as well, and it rounds off the negative numbers and returns the result.

#applying round function on negative numbers

num = -3.98

print(round(num,1))

num = -2.8

print(round(num))

num = -5.67989

print(round(num,2))

num = -100.9843

print(round(num,1))

num = -20.04

print(round(num))

num = -32.0908

print(round(num,3))

num = -3.9898

print(round(num))

Output

Conclusion

The round() is a built-in function of Python that rounded off the floating-point number to the given decimal numbers. It is a very useful function when you are performing the numbers related task. This article briefly explains the round() function with examples.



from Linux Hint https://ift.tt/2Tqr3Xl

Post a Comment

0 Comments