Table of Contents
Truncating a floating-point number is a widely used practice and helps in making the calculations and results easier. The process of truncating a float can be tackled in two ways, one by removing the excess and unnecessary digits or the other by simply rounding the number.
This article focuses on the different ways available to truncate float in Python.
What is a float in Python?
A float is one of the two number types provided in Python and is a data type that is utilized for storing a floating-point number or a number that contains a decimal point.
How to truncate float in Python?
Using f-strings to truncate float in Python.
String formatting provides a medium for loads of customizations on string variables in the python code. The customizations can be manually chosen and done by the programmer. We use the %
sign, which is commonly regarded as the interpolation operator for implementing string formatting in Python.
The f-strings were introduced starting from the version Python 3.6 and provide a fresh and improved approach to carry out the process of string formatting. The f-strings are known to be faster and more concise than their older counterparts.
The following code uses f-strings to truncate float in Python.
1 2 3 4 5 6 |
x = 1.256998 txt = f"{x:.2f}" y = float(txt) print(y) |
The above code provides the following output:
We should note that the output here is 1.26
, which means that the number has been rounded off automatically. The f-strings are a smart and efficient way of string formatting and thus, enable the automatic rounding off of the given number.
Explanation:
- As f-strings are used on strings, the given floating-point number is converted to a string.
- Then, we look at the syntax
f"{field_name:[.precision][type]}"
and specify the given number in thefield_name
, and the desired number places for truncating in place of theprecision
space respectively. - Then, the
float()
function is called with the corresponding f-string as an argument to the function. - The received float variable is then displayed as the output.
Further reading:
Using the int()
function to truncate float in Python.
We can make use of the int()
function to truncate float in Python. However, its working is not all straightforward and is explained below.
To simply drop the unnecessary characters from the floating-point number, we will multiply the given number with 10**x
with x
being the number of digits after the decimal place that we want to be left with at the end. The number is then divided with the same value 10**x
and converted back to float.
The following code uses the int()
function to truncate float in Python.
1 2 3 4 5 6 |
def trunc(a, x): int1 = int(a * (10**x))/(10**x) return float(int1) print(trunc(1.256998,2)) |
The above code provides the following output:
Explanation:
- A user-defined function is created, where the given float is first converted to an integer and is multiplied with
10**x
as explained above. - Then, it is divided by the same value.
- This resulting integer variable is then converted back to float.
- The output is then displayed by calling out the created function.
Using the str()
function to truncate float in Python.
In this method, the float is first converted to a string and is then worked upon.
We simply search for a decimal point in the resultant string after converting the float to a string variable. If the decimal point exists, we specify x
which are the digits to be saved after this decimal place.
The following code uses the str()
function to truncate float in Python.
1 2 3 4 5 6 7 8 9 10 11 12 |
def trunc(a,x): temp = str(a) for i in range(len(temp)): if temp[i] == '.': try: return float(temp[:i+x+1]) except: return float(temp) return float(temp) print(trunc(1.256998,2)) |
The above code provides the following output:
Explanation:
- A user-defined function is created, and the float value is converted to a string.
- The resultant string
temp
is checked as to whether it contains a decimal place or not. - If it does, then with the help of the
try...catch
statements, the unnecessary decimal places are removed. - The string is finally converted back to float at the end of the function.
- The result is displayed by calling the created function.
Using the round()
function to truncate float in Python.
The round()
function is a predefined function that is generally utilized to round off any given floating-point number. It takes in two parameters, the number which is to be operated on, and the decimal places that we want to round the number off to.
The following code uses the round()
function to truncate float in Python.
1 2 3 |
print(round(1.256998,2)) |
The above code provides the following output:
We should note that unlike the previous two methods mentioned above, this method rounds off the given floating-point number along with carrying out the process of truncating it.
That’s all about how to truncate float in Python.