In this tutorial, you’ll learn how to use Python to truncate a float, to either no decimal places or a certain number of decimal places. You’ll learn how to do this using the built-in int()
function, the math
library, and string methods, including Python f-strings. You’ll also learn how to truncate a list of floats.
Being able to truncate values in Python has a number of important benefits, such as being able to output numbers in a more presentation-ready format. Being able to output values in a more reader friendly format is an important skill to learn in order to make your data easier to look at.
The Quick Answer: Use math.trunc()
Table of Contents
Use the int Function to Truncate a Float in Python
The built-in int()
function takes a float and converts it to an integer, thereby truncating a float value by removing its decimal places.
The int()
function works differently than the round()
and floor()
function (which you can learn more about here). The function only removes anything following the decimal, regardless of what follows it. This is differently than the round()
function, as it does not round up. It’s also different from the floor()
, which returns an integer now greater than a number.
Let’s see how we can use the int()
function to truncate a float in Python:
# Truncate a float in Python with int()
float1 = 123.356
float2 = -2434.545
print(int(float1))
print(int(float2))
# Returns:
# 123
# -2434
We can see here that anything following the decimal place is truncated.
In the next section, you’ll learn how to use the math
library to truncate a float in Python.
Want to learn how to use the Python zip()
function to iterate over two lists? This tutorial teaches you exactly what the zip()
function does and shows you some creative ways to use the function.
Use the Math Library to Truncate a Float in Python
Similar to the int()
function, the math
library has a library that can be helpful to truncate a float in Python, the trunc()
function.
While it may seem trivial to use another library to do this, it can help your code read a little clearer to explain what the function is doing and why it’s doing it.
Because our intention is to truncate a float, the trunc()
function makes this explicitly clear, whereas the int()
function doesn’t.
Let’s see how we can use the trunc()
function to truncate some numbers in Python:
# Truncate a float in Python with math.trun()
import math
float1 = 123.356
float2 = -2434.545
print(math.trunc(float1))
print(math.trunc(float2))
# Returns:
# 123
# -2434
We can see here that the same result is returned as when using the int()
function.
In the next section, you’ll learn how to use string methods to truncate a float in Python.
Want to learn more about calculating the square root in Python? Check out my tutorial here, which will teach you different ways of calculating the square root, both without Python functions and with the help of functions.
Use String Methods to Truncate a Float in Python
We can also use string methods to truncate a float in Python. This works by first converting a float to string and then manipulating the string.
Using the str.split()
method, we can split a string at a period and keep only the first section of the split. You can learn more about splitting strings in this tutorial.
Let’s see how we can truncate a float in Python by using the str.split()
method.
# Truncate a float in Python with str.split()
float1 = 123.356
float2 = -2434.545
print(str(float1).split('.')[0])
print(str(float2).split('.')[0])
# Returns:
# 123
# -2434
Let’s see how this works:
- We first convert the float to a string
- We then split the string using the
.
character - Finally, we return the first item returned (the piece preceding the decimal)
In the next section, you’ll learn how to use f-strings to truncate a float in Python.
Want to learn more about calculating the square root in Python? Check out my tutorial here, which will teach you different ways of calculating the square root, both without Python functions and with the help of functions.
Use f-strings to Truncate a Float in Python
There may be times that you want to truncate a float to a certain number of decimal points. This can be helpful, say, when displaying currencies or percentages.
For this, we can easily use string formatting by way of the helpful f-string features in Python. With this, we can specify the number of decimal points to keep and convert the string back to a float.
# Truncate a float in Python with f-strings
float1 = 123.356
float2 = -2434.545
# Keep two decimal places
print(float(f'{float1:.2f}'))
print(float(f'{float2:.2f}'))
# Returns:
# 123.36
# -2434.55
We can see here that we first convert the float to a string, returning only a certain number of decimal places. We then convert the string back to a float, using the float()
function. It’s important to note here that this will round the number, not truncate it directly. This may lead to potentially unwanted results.
Want to learn more about Python f-strings? Check out my in-depth tutorial, which includes a step-by-step video to master Python f-strings!
Truncate a List of Floats in Python Using a For Loop
There may be times when you are given a list of floats and need truncate all of them. One of the ways in which you can do this is by using a Python for loop.
When doing this, we can loop over each item in the list, truncate it, and add it to a new list.
Let’s see what this looks like in Python:
# Truncate a list of floats using a for loop
floats = [1.234, 45.543, 132.87, 76.989]
truncated = list()
for float in floats:
truncated.append(int(float))
print(truncated)
# Returns: [1, 45, 132, 76]
What we’ve done here is:
- Instantiated a new, empty list to hold our truncate values
- Looped over each item in our list of floats and truncated it using the
int()
function - Appended the truncated item to our list
We can also use a list comprehension to make this process a little bit easier, saving us the step of instantiating a new list first.
Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.
Truncate a List of Floats in Python Using a List Comprehension
The above section showed how to use a Python for loop to truncate a list of floats. We can also use a Python list comprehension to do this, thereby saving us some visual clutter and the need to instantiate an empty list first.
Let’s see how we can use a list comprehension to accomplish this:
# Truncate a list of floats using a list comprehension
floats = [1.234, 45.543, 132.87, 76.989]
truncated = [int(item) for item in floats]
print(truncated)
# Returns: [1, 45, 132, 76]
To learn more about Python list comprehensions, check out the image below that shows how they work:
Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.
Conclusion
In this tutorial, you learned how to use Python to truncate a float. You learned how to do this using the int()
function, the math
library, and string methods. You also learned how to use for loops and list comprehensions to truncate a list of floating point values in Python.
To learn more about the math.trunc()
function, check out the official documentation here.
Your “Use f-strings to Truncate a Float in Python” is wrong. The `f'{float1:.2f}’` notation “rounds” rather than “truncates”.
Thanks Jamez! I have fixed that. I appreciate you letting me know.