How to Round Numbers in Python 


               


How to Round Numbers in Python



Python


In this article, we explain how to round numbers in Python.

We will show how to round numbers to a certain number of decimal places and how to round a number to the nearest integer.

So to round numbers in Pytho, we use the round() function.

The round() function takes 2 parameters.

The first parameter is the number to be rounded.

And the second parameter is the number of decimal places to round the number to.

Below we illustrate the round() function.



So the code above is very basic and illustrates the round() function in Python.

We create a variable named var1 that is set equal to, 456.383920819304

We then round this variable to 1 decimal place.

Notice how the round() function takes 2 parameter.

The first parameter is the number that will be rounded.

The second parameter is the number of decimal places to round the number to.

456.383920819304 rounded to 1 decimal place is 456.4

We then round the number to 2 decimal places, which gives us, 456.38

We then round the number to 4 decimal places, which gives us 456.3839

We then round the number to the nearest integer with the statement, round(var1)

When you use the round() function and simply place inside of it the number to be rounded, Python rounds the number to the nearest integer. 456.383920819304 rounded to the nearest integer is 456.

And this is all that is needed to round numbers in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...