This article will show you how to round numbers up, down, and to the nearest 1, 10 100 (or any number) in the Python programming language.
Truncation vs. Rounding
Truncation isn’t technically rounding, but it serves a similar purpose, so I’ll include it here.
It’s the simplest way to shorten a number – just cutting off the characters at the end until it’s in the format you require.
Rounding gets the nearest number, whereas truncation cuts off digits from the number.
To round 2.9 to the nearest whole number would give 3 To truncate 2.9 to the nearest whole number would give 2
Using The math.trunc() Method to Return the Truncated Integer Part of a Number
The math.trunc() method is part of the Python math library and performs truncation to the nearest integer (whole number). Here it is in action:
# Import the math library import math # print a truncated number print(math.trunc(3.7)) # Will print the number 3
As it is simply removing all information after the decimal place, it will round any negative number up and any positive number down.
Truncation to a Set Number of Decimals
Python lacks a built-in function for truncating to an arbitrary number of decimals (rather than just the nearest integer as above).
This function fills this role – you can copy and paste it straight into your code.
# Import the math library import math # define a function to truncate a number to a given number of decimal places (defaulting to 0 decimal places) def truncateToDecimals(number, decimals=0): if not isinstance(decimals, int): raise TypeError("decimals places must be an integer.") elif decimals < 0: raise ValueError("decimals must be greater than or equal to 0") if decimals == 0: return math.trunc(number) return math.trunc(number * 10.0 ** decimals) / 10.0 ** decimals
Here it is in use:
truncateToDecimals(6.76666, 3) # Will return 6.766
Rounding to A Specified Number of Decimal Places
The Python round() function can be used to round a number to a set number of decimal places.
Here it is in action:
round(8.9873, 2) # Will return 8.99
The round() function accepts 2 parameters – the number to round followed by the number of decimal places to round to. If the number of decimal places is not supplied, it defaults to 0.
Using math.ceil() to Round Up To The Nearest Integer
The math.ceil() method rounds a given number up to the nearest integer.
math.ceil(2.4) # Will return 3 math.ceil(-3.9) # will return -3
It gets the nearest integer that is greater than or equal to the number given to it.
Using math.floor() to Round Down To The Nearest Integer
Conversely, math.floor() rounds a given number down to the nearest integer:
math.floor(2.4) # Will return 2 math.floor(-3.9) # Will return -4
It gets the nearest integer that is less than or equal to the number given to it.
Rounding to the Nearest 2, 10, 5, Whatever
The below functions will accept a number and round it to the nearest whole number in whatever base you want, defaulting to base 10:
In Python 2
def roundToNearest(inputNumber, base=10): return int(base * round(float(inputNumber)/base))
In Python 3
def roundToNearest(inputNumber, base=10): return base * round(inputNumber/base)
This can be useful if rounding to calculate change for a currency in which the smallest denomination coin is larger than the smallest unit of currency (for example, Australia’s smallest value coin is 5 cents, but the currency allows for prices in 1 cent increments).
That pretty much covers the main rounding methods for Python. There are several popular math and data processing libraries for Python (like NumPy) – I’ll dig into some of the functionality of those in future articles – be sure to subscribe to our Twitter to stay up to date.
Meanwhile, now that you have some rounded numbers why not plot them on a scatter plot?