Skip to content
Home » How To Round Numbers in Python

How To Round Numbers in Python

Spread the love

Rounding numbers is such a common tasks that Python lets you do that out of the box. There’s a built-in function for that, round. So, how do you round numbers in Python?

To round the number x to n decimal places we use the function like this:

round(x, n) – x rounded to n decimal places

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

The parameter n is optional. Its default value is 0, so round(x) has the same meaning as round(x, 0).

Some examples:

>>> round(3.5478745) 
4
>>> round(3.5478745, 2)
3.55
>>> round(-1.12)
-1
>>> round(-1.12, 5)
-1.12
>>> round(-6.32441333547541)
-6
>>> round(-6.32441333547541, 1)
-6.3

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version:


Spread the love

Leave a Reply