The round() function is one of the built-in functions in Python. This function is used to round the decimal points to the user-specified number. If the user does not give input, then the round function converts the float into an integer.
Let’s take a look at code.
Rounding Integer Value
num = 5.2234232 print(round(num)) |
The following is the output.
Round function to a specified number.
The first input to the Python round function is the number that the user wants to round. The second input is the number of digits the user wants after the decimal point.
num = 5.1234567 print(round(num,2)) print(round(num,3)) print(round(num,4)) |
The following is the output of the above code.
Rounding Negative Values
Rounding negative values is the same as a positive value.
m = -5.1234567 print(round(num)) print(round(num,2)) print(round(num,3)) print(round(num,4)) |
Rounding Integer
When we try to round integer, the result remains the same. Let’s have a look at the code.
num = 4 print(round(num)) print(round(num,2)) print(round(num,3)) print(round(num,4)) |