Scripting

Python round() Function

Introduction

The round() function in Python is used to round a decimal value to the nearest multiple of 10. You can also specify to what number of decimal places the function rounds. If the number is less than 5, the number next to it will be preserved, if it is greater than 5, the number next to it will be rounded up to 1 unit.

Now we’re gonna guide you about using the round() function in Python. Hope you understand.

Example

x = round(3.475, 2)

print(x)

Output:

3.48

Definition

The round() function rounds to the decimal place you specify.

If the number is less than 5, the number next to it will be preserved, if it is greater than 5, the number next to it will be rounded up to 1 unit.

The syntax

round(number, digits)

Parameter Values:

number: number of decimals you want to round (Required)

digits: the decimal place you want to round to. Default is 0 (Optional)

More examples

Example 1: Basic round() function

print(round(13.4))

print(round(12.6))

print(round(10.3))

Output:

13

13

10

Example 2: Specify rounding position

x = round(13.4567, 2)

y = round(12.23456, 3)

print(x)

print(y)

Output:
13.46

12.235

Example 3: Python round() up

print(round(11.6))

print(round(14.7))

Output:

12

15

Example 4: Python round() down

print(round(11.3))

print(round(14.5))

Output:

11

14

Conclusion

And we guided you on how to use the round() function in Python.

Thank you for checking it out!

Similar Posts