04-28-2022, 10:51 AM
How to Round a Number Down in Python?
Problem Formulation: Given a float number. How to round the float down in Python?
Here are some examples of what you want to accomplish:
Solution: If you have little time, here’s the most straightforward answer:
To round a positive or negative number down in Python, apply integer division to and divide by . Specifically, the expression will first perform normal float division and then throw away the remainder—effectively “rounding down”.
In general, there are multiple ways to round a float number down in Python:
Let’s dive into each of those and more options in the remaining article. I guarantee you’ll get out of it having learned at least a few new Python tricks in the process!
Method 1: Integer Division (x//1)
The most straightforward way to round a positive or negative number down in Python is to use integer division by . The expression will first perform normal division and then skip the remainder—effectively “rounding down”.
For example:
This trick works for positive and negative numbers—beautiful isn’t it?![[Image: 1f33b.png]](https://s.w.org/images/core/emoji/13.1.0/72x72/1f33b.png)
Here are a couple of Python code examples:
Info: The double-backslash operator performs integer division and the single-backslash operator performs float division. An example for integer division is . An example for float division is .
Feel free to watch the following video for some repetition or learning:
Method 2: math.floor()
To round a number down in Python, import the library with , and call .
The function returns the floor of the specified that is defined as the largest integer less than or equal to .
Note: The function correctly rounds down floats to the next-smaller full integer for positive and negative integers.
Here’s a code example that rounds our five numbers down to the next-smaller full integer:
The following video shows the as well as the functions — feel free to watch it to gain a deeper understanding:
Method 3: np.floor()
To round a number down in Python, import the NumPy library with, and call .
The function returns the floor of the specified that is defined as the largest integer less than or equal to .
Here’s an example:
Both and round down to the next full integer. The difference between and is that the former returns an integer and the latter returns a float value.
Method 4: int(x)
Use the function to round a positive number down to the next integer. For example, rounds down to the answer .
Here’s an example for positive numbers where will round down:
However, if the number is negative, the function will round up! Here’s an example for negative numbers:
Before I show you how to overcome this limitation for negative numbers, feel free to watch my explainer video on this function here:
Method 5: int(x) – bool(x%1)
You can also use the following vanilla Python snippet to round a number down to the next full integer:
Explanation: Any non-zero expression passed into the function will yield which is represented by integer 1.
The modulo expression returns the decimal part of .
Here’s what this looks like in a simple Python function:
Alternatively, you can use the following slight variation of the function definition:
Method 6: round()
This method is probably not exactly what you want because it rounds a number up and down, depending on whether the number is closer to the smaller or larger next full integer. However, I’ll still mention it for comprehensibility.
Python’s built-in function takes two input arguments:
It rounds the number to the given precision and returns the result. The return value has the same type as the input number—or integer if the argument is omitted.
Per default, the precision is set to 0 digits, so results in .
Here are three examples using the function—that show that it doesn’t exactly solve our problem.
Again, we have a video on the function — feel free to watch for maximum learning!
Python One-Liners Book: Master the Single Line First!
Python programmers will improve their computer science skills with these useful one-liners.
![[Image: 3D_cover-1024x944.jpg]](https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-1024x944.jpg)
Python One-Linerswill teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.
The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.
Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.
You’ll also learn how to:
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.
Get your Python One-Liners on Amazon!!
Problem Formulation: Given a float number. How to round the float down in Python?
Here are some examples of what you want to accomplish:
- Code:
42.52 --> 42
- Code:
21.99999 --> 22
- Code:
-0.1 --> -1
- Code:
-2 --> -2
Solution: If you have little time, here’s the most straightforward answer:
To round a positive or negative number
Code:
xCode:
//Code:
xCode:
1Code:
x//1Code:
xIn general, there are multiple ways to round a float number
Code:
x- Vanilla Python: The expression will first perform normal division and then skip the remainder—effectively “roundingCode:
x//1down”.Code:x
- Round down: The function rounds numberCode:
math.floor(x)down to the next full integer.Code:x
- Round down (float representation): Alternatively, rounds down and returns a float representation of the next full integer (e.g.,Code:
numpy.floor(x)instead ofCode:2.0).Code:2
- Round up: The function rounds numberCode:
math.ceil(x)up to the next full integer.Code:x
- Round up and down: The Python built-in function roundsCode:
round(x)up and down to the closest full integer.Code:x
Let’s dive into each of those and more options in the remaining article. I guarantee you’ll get out of it having learned at least a few new Python tricks in the process!
Method 1: Integer Division (x//1)
The most straightforward way to round a positive or negative number
Code:
xCode:
//Code:
1Code:
x//1Code:
xFor example:
- Code:
42.52//1 == 42
- Code:
21.99//1 == 21
- Code:
-0.1//1 == -1
- Code:
-2//1 == -2
This trick works for positive and negative numbers—beautiful isn’t it?
![[Image: 1f33b.png]](https://s.w.org/images/core/emoji/13.1.0/72x72/1f33b.png)
Here are a couple of Python code examples:
Code:
def round_down(x): return x//1 print(round_down(42.52))
# 42 print(round_down(21.99999))
# 21 print(round_down(-0.1))
# -1 print(round_down(-2))
# -2
Info: The double-backslash Code:
//Code:
/Code:
40//11 = 3Code:
40/11 = 3.6363636363636362Feel free to watch the following video for some repetition or learning:
Method 2: math.floor()
To round a number down in Python, import the
Code:
mathCode:
import mathCode:
math.floor(number)The function returns the floor of the specified
Code:
numberCode:
number
Note: The Code:
math.floor()Here’s a code example that rounds our five numbers down to the next-smaller full integer:
Code:
import math print(math.floor(42.52))
# 42 print(math.floor(21.99999))
# 21 print(math.floor(-0.1))
# -1 print(math.floor(-2))
# -2The following video shows the
Code:
math.floor()Code:
math.ceil()Method 3: np.floor()
To round a number down in Python, import the NumPy library with
Code:
import numpy as npCode:
np.floor(number)The function returns the floor of the specified
Code:
numberCode:
numberHere’s an example:
Code:
import numpy as np print(np.floor(42.52))
# 42.0 print(np.floor(21.99999))
# 21.0 print(np.floor(-0.1))
# -1.0 print(np.floor(-2))
# -2.0Both
Code:
math.floor()Code:
np.floor()Code:
math.floor()Code:
np.floor()Method 4: int(x)
Use the
Code:
int(x)Code:
x>0Code:
int(42.99)Code:
42.99Code:
42Here’s an example for positive numbers where
Code:
int()Code:
print(int(42.52))
# 42 print(int(21.99999))
# 21However, if the number is negative, the function
Code:
int()Code:
print(int(-0.1))
# 0 print(int(-2))
# -2Before I show you how to overcome this limitation for negative numbers, feel free to watch my explainer video on this function here:
Method 5: int(x) – bool(x%1)
You can also use the following vanilla Python snippet to round a number
Code:
x- If is positive, round down by callingCode:
x.Code:int(x)
- If is negative, round up by callingCode:
x.Code:int(x) - bool(x%1)
Explanation: Any non-zero expression passed into the
Code:
bool()Code:
TrueThe modulo expression
Code:
x%1Code:
x- If it is non-zero, we subtract , i.e., we round down.Code:
bool(x%1) == 1
- If it is zero (for whole numbers), we subtract , i.e., we’re already done.Code:
bool(x%1) == 0
Here’s what this looks like in a simple Python function:
Code:
def round_down(x): if x<0: return int(x) - bool(x%1) return int(x) print(round_down(42.52))
# 42 print(round_down(21.99999))
# 21 print(round_down(-0.1))
# -1 print(round_down(-2))
# -2Alternatively, you can use the following slight variation of the function definition:
Code:
def round_down(x): if x<0: return int(x) - int(x)!=x return int(x)This method is probably not exactly what you want because it rounds a number up and down, depending on whether the number is closer to the smaller or larger next full integer. However, I’ll still mention it for comprehensibility.
Python’s built-in
Code:
round()- a andCode:
number
- an optional in decimal digits.Code:
precision
It rounds the number to the given precision and returns the result. The return value has the same type as the input number—or integer if the
Code:
precisionPer default, the precision is set to 0 digits, so
Code:
round(3.14)Code:
3Here are three examples using the
Code:
round()Code:
import math print(round(42.42))
# 42 print(round(21.00001))
# 21 print(round(-0.1))
# 0Again, we have a video on the
Code:
round()Python One-Liners Book: Master the Single Line First!
Python programmers will improve their computer science skills with these useful one-liners.
![[Image: 3D_cover-1024x944.jpg]](https://blog.finxter.com/wp-content/uploads/2020/06/3D_cover-1024x944.jpg)
Python One-Linerswill teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.
The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.
Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.
You’ll also learn how to:
- Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
- Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
- Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
- Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
- Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting
By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.
Get your Python One-Liners on Amazon!!

