python

How to Round Off Numbers in Python


In this article, you will learn how to round off numbers in Python.

Let’s say you have a float variable with value 123.45

# Import the math library
import math

a = 123.45

In order to round off numbers, you can use the math.ceil() method or math.floor() method. In this example, you will be rounding up and down the float variable ‘a’.

# Import the math library
import math

a = 123.45

# Round up a number to its nearest integer
print(math.ceil(a))
# => 124

# Round down a number to its nearest integer
print(math.floor(a))
# => 123

Note: The math.ceil() method functions by rounding up a number to its nearest integer. The math.floor() method functions by rounding down a number to its nearest integer.


Share on social media

//