How to Round Numbers in Python?
Last Updated :
30 Apr, 2025
Rounding a number simplifies it while keeping its value as close as possible to the original. Python provides various methods to round numbers, depending on how we want to handle the precision or rounding behavior. In this article, we’ll cover the most commonly used techniques for rounding numbers in Python, along with examples. For example, Input is 3.5 then Output should be 4.
Using Built-in round() Function
Python’s built-in round() function rounds a number to a given precision. If no precision is specified, it rounds to the nearest integer.
[GFGTABS]
python
print(round(11))
print(round(22.7))
print(round(4.465, 2))
print(round(4.476, 2))
print(round(4.473, 2))
[/GFGTABS]
Output
11
23
4.46
4.48
4.47
Explanation:
- round(x) rounds x to the nearest integer.
- round(x, n) rounds x to n decimal places.
Using Truncation (Cutting Off Digits)
Truncation cuts off digits after a specified number of decimal places without rounding. It works for both positive and negative numbers.
[GFGTABS]
python
def truncate(num, dec=0):
mult = 10 ** dec
return int(num * mult) / mult
print(truncate(16.5))
print(truncate(-3.853, 1))
print(truncate(3.815, 2))
print(truncate(346.8, -1))
print(truncate(-2947.48, -3))
[/GFGTABS]
Output
16.0
-3.8
3.81
340.0
-2000.0
Explanation:
- Multiplies by 10^dec, truncates, and divides back.
- Useful when you only want to chop off digits, not round.
Using math.ceil() and math.floor()
Python’s math module provides two essential functions:
- ceil(x): Rounds up to the nearest integer.
- floor(x): Rounds down to the nearest integer.
[GFGTABS]
python
import math
print(math.ceil(4.2))
print(math.ceil(-0.5))
print(math.floor(2.2))
print(math.floor(-0.5))
[/GFGTABS]
Explanation:
- ceil(x) always rounds up.
- floor(x) always rounds down, even for negative numbers.
Round Up to Specific Decimal Places
If we want to round up to a specified decimal place, you can combine multiplication, math.ceil(), and division.
[GFGTABS]
python
import math
def round_up(num, dec=0):
mult = 10 ** dec
return math.ceil(num * mult) / mult
print(round_up(2.1))
print(round_up(2.23, 1))
print(round_up(2.543, 2))
print(round_up(22.45, -1))
print(round_up(2352, -2))
[/GFGTABS]
Output
3.0
2.3
2.55
30.0
2400.0
Explanation:
- Shifts decimal right, applies ceil(), and shifts back.
- Works for positive and negative numbers.
We can follow the diagram below to understand round up and round down. Round up to the right and down to the left.

Rounding up always rounds a number to the right on the number line and rounding down always rounds a number to the left on the number line.
Round Down to Specific Decimal Places
Similarly, for rounding down we can use floor() function instead of ceil():
[GFGTABS]
python
import math
def round_down(num, dec=0):
mult = 10 ** dec
return math.floor(num * mult) / mult
print(round_down(2.5))
print(round_down(2.48, 1))
print(round_down(-0.5))
[/GFGTABS]
Explanation: Same shifting logic as round up, but using floor().
Round Numbers Using NumPy
NumPy provides the np.round() function for rounding arrays or numbers efficiently.
[GFGTABS]
Python
import numpy as np
arr = np.array([1.234, 2.567, 3.789])
print("Nearest integer:", np.round(arr))
print("Two decimals:", np.round(arr, 2))
[/GFGTABS]
Output
Nearest integer: [1. 3. 4.]
Two decimals: [1.23 2.57 3.79]
Explanation:
- np.round(arr) rounds to the nearest whole number.
- np.round(arr, n) rounds to n decimals.
Round Numbers in Python using Rounding Bias concept
When rounding large datasets, we must avoid rounding bias. Python supports several rounding methods for that and most generally used methods are:
1. Rounding Half Up
Always rounds .5 upward.
[GFGTABS]
python
import math
def round_half_up(num, dec=0):
mult = 10 ** dec
return math.floor(num * mult + 0.5) / mult
print(round_half_up(1.28, 1))
print(round_half_up(-1.5))
print(round_half_up(-1.225, 2))
[/GFGTABS]
Explanation: Adds 0.5 before flooring to simulate “round half up.”
2. Rounding Half Down
Always rounds .5 downward.
[GFGTABS]
python
import math
def round_half_down(num, dec=0):
mult = 10 ** dec
return math.ceil(num * mult - 0.5) / mult
print(round_half_down(2.5))
print(round_half_down(-2.5))
print(round_half_down(2.25, 1))
[/GFGTABS]
Explanation: Subtracts 0.5 before applying ceil() to simulate “round half down.”
Rounding Half to Even (Bankers’ Rounding)
Python’s built-in round() uses round half to even strategy, which rounds .5 to the nearest even number to minimize bias.
- If n is positive and d >= 5, round up
- If n is positive and d = 5, round down
- If n is negative and d >= 5, round down
- If n is negative and d < 5, round up
After rounding as per the rules mentioned above, we can shift the decimal place back to the left.
[GFGTABS]
python
from decimal import Decimal
print(Decimal("0.1"))
print(Decimal(0.1))
print(Decimal("1.65").quantize(Decimal("1.0")))
print(Decimal("1.675").quantize(Decimal("1.00")))
[/GFGTABS]
Output
0.1
0.1000000000000000055511151231257827021181583404541015625
1.6
1.68
Explanation:
- Decimal offers precise control over decimal rounding.
- quantize() rounds to specified decimal places with minimal floating-point error.
Related articles:
Similar Reads
Random Numbers in Python
Python defines a set of functions that are used to generate or manipulate random numbers through the random module. Functions in the random module rely on a pseudo-random number generator function random(), which generates a random float number between 0.0 and 1.0. These particular type of functions
7 min read
numpy.round_() in Python
The round_() function in NumPy rounds the elements of an array to a specified number of decimal places. This function is extremely useful when working with floating-point numbers and when precision is important in scientific computing or data analysis. Syntax: numpy.round_(arr, decimals=0, out=None)
3 min read
How to clamp floating numbers in Python
Clamping is a method in which we limit a number in a range or in between two given numbers. When we clamped a number then it holds the value between the given range. If our clamped number is lower than the minimum value then it holds the lower value and if our number is higher than the maximum value
2 min read
Format a Number Width in Python
Formatting numbers to a fixed width is a common requirement in various programming tasks, especially when dealing with data presentation or storage. By understanding these techniques, developers can ensure consistent and visually appealing formatting of numerical data in their Python. Format a Numbe
3 min read
numpy.rint() in Python
The numpy.rint() is a mathematical function that rounds elements of the array to the nearest integer. Syntax : numpy.rint(x[, out]) = ufunc ârintâ) Parameters : array : [array_like] Input array. Return : An array with all array elements being rounded off, having same type and shape as input. Code #1
2 min read
Words to Numbers Converter Tool in Python
In this tutorial, we will guide you through the process of building a Word-to Numbers converter using Python. To enhance the user experience, we will employ the Tkinter library to create a simple and intuitive Graphical User Interface (GUI). This tool allows users to effortlessly convert words into
2 min read
numpy.trunc() in Python
The numpy.trunc() is a mathematical function that returns the truncated value of the elements of array. The trunc of the scalar x is the nearest integer i which, closer to zero than x. This simply means that, the fractional part of the signed number x is discarded by this function. Syntax : numpy.tr
2 min read
How to format numbers as currency strings in Python
Formatting numbers as currency strings in Python is a common requirement especially in the applications involving financial data. Formatting numbers as currency strings in Python involves presenting numeric values in the standardized currency format typically including the currency symbol, thousands
3 min read
How to Add leading Zeros to a Number in Python
In this article, we will learn how to pad or add leading zeroes to the output in Python. Example:Input: 11 Output: 000011 Explanation: Added four zeros before 11(eleven).Display a Number With Leading Zeros in PythonAdd leading Zeros to numbers using format() For more effective handling of sophistica
3 min read
Python | Numpy matrix.round()
With the help of Numpy matrix.round() method, we are able to round off the values of the given matrix. Syntax : matrix.round() Return : Return rounded values in matrix Example #1 : In the given example we are able to round off the given matrix by using matrix.round() method. # import the important m
1 min read