There is an inbuilt function present in Python as Round(). This function is used to round off any given number of digits and returns the floating points number. In this post, you will learn how to Round in Python for any numbers.
round(number, number of digits)
number = number you want to round off.
number of digits = number of digits up to which you want the given number to be rounded off.
Parameters of Round() in Python
As you can see the round() function needs parameters. But since even if you miss any one of the parameters the given number will be treated as the number.
- Suppose only an integer as given number is provided for example 10, then it will round off 10.
- Else when a decimal number is provided to round off funtion then it will round off the ceil integer after that if the decimal value is greater than or equal to 5. And it will round off to the floor integer if the decimal is less than 5.
Let us take an example of the round function implementation with the use of the python code. Below are the various input and output you can expect from Python when using the Round function.
How to Use Round in Python
Let us look into the examples for the Round function and the use of round in Python.
#for interger numbers print(round(10)) #for floating point numbers print(round(32.8)) print(round(32.5)) print(round(32.4))
10 33 33 32
Hence as shown in the above examples you can see that the round function was only given the first parameter and the second parameter is missing hence the output is the only integer without any decimal.
Below is the python implementation of the round() function if the second parameter is present
# when the (secondparameter+1)th digit in the number of first parameter is =5 print(round(4.785, 2)) # when the (secondparameter+1)th digit in the number of first parameter is greater than 5 print(round(4.786, 2)) # when the (secondparameter+1)th digit in the number of first parameter is less than 5 print(round(4.783, 2))
4.79 4.79 4.78
As you can see in the above example we are able to round the number in python to two decimal places. Hence we can use this round function to get the given decimal number rounded to two decimal places as well. Though any decimal places, you would like.
Rounding off numbers is usually done in any application when you want to handle the mismatch of values with decimal values and fractions.
One of the examples of practical applications can be the currency exchanges where there is the only use of numbers all the time. Having two different numbers with different decimal values will not be equitable easily.
Hence it is always good to round off the numbers to two or three digits decimal places. This lead to better calculations and comparisons for the application.
How to Round Number in Python to Two Decimal Places
As we already showed you many examples above for two decimal places number being rounded off. It is quite easier to do it. All you need to make sure that in the Round() function you mention the first parameter as the decimal number that you want to round off and in the second parameter the decimal places you want to round.
So now if you want to round for example a number 1.56725673 to two decimal places then you need to use the code as below for it to round the number to two decimal places.
How to Round to Three Decimal Places
Very easy again as illustrated above. Suppose the number is
1.56725673 and you want to round this number to three decimal places then you need to set the second parameter of the function to 3.
As you learned how to round a number in python to many decimal places you want. Now you can use the round function to get any digit decimal place of a rounded number easily.
This simplifies the comparison of two or more decimal numbers if you are going to use the decimal numbers in your application. Since comparison of two decimal numbers without being rounded off can lead to error or failed test cases.
If you wanna check more Python-related posts then Read Prime Generator in Python.