×


Python round() Function in Linux



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.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Python function queries.

In this context, we shall look into using the round() function in Python.


What is the round() function in Python ?

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

Basically, 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.


What is the syntax for Python round() Function ?

It's syntax is given below:

round(number, digits)


Python round() Function Parameter Values:

  • number: number of decimals you want to round (Required).
  • digits: the decimal place you want to round to. Default is 0 (Optional).


Examples of using round() Function

1. Take a look at the below function:

x = round(3.475, 2)
print(x)

It's output will give:

3.48


2. Basic round() function:

print(round(13.4))
print(round(12.6))
print(round(10.3))

It's Output will give:

13
13
10


3. Specify rounding position:

x = round(13.4567, 2)
y = round(12.23456, 3)
print(x)
print(y)

The Output will give:

13.46
12.235


4. Python round() up:

print(round(11.6))
print(round(14.7))

It's Output will give:

12
15


5. Python round() down:

print(round(11.3))
print(round(14.5))

It's Output will give:

11
14


[Need help in fixing Python function issues ? We can help you. ]


Conclusion

This article covers how to use the round() function in Python. In fact, the round() function returns a floating-point number rounded to the specified number of decimals.


round() Return Value

The round() function returns the:

  • nearest integer to the given number if ndigits is not provided.
  • number rounded off to the ndigits digits if ndigits is provided.

More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com