Truncate a float python – How to Truncate a float in Python?

Truncate a float python: In Python, we can truncate float to specific decimal places or to no decimal places. We can accomplish this in a variety of ways, including utilizing the int() function, the math library, and other methods.

There are various advantages to truncating a float in Python, including the ability to represent output in a more appealing and presentation-ready manner, making the output more user-friendly and easy to look at.

Truncating a float in Python

Truncate float python: There are various methods to truncate a float in Python. Some of them are:

  1. Using math library
  2. Using int() function
  3. Using string methods
  4. Using For Loop
  5. Using f-strings
  6. Using List comprehension

1)Using math library:

Python truncate float: Python’s math library contains a function that can be used to truncate a float. To truncate a float, we utilize the trunc() method.

When you use trunc() function, the float value is truncated to an integer value.

Approach:

  • Import math module using the import keyword.
  • Give the first number as static input and store it in a variable
  • Give the second number as static input and store it in another variable
  • Truncate the given first number to 0 decimal places or an integer using the trunc() function of the math module and print the result.
  • Similarly, truncate the given second number.
  • The Exit of the Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the first number as static input and store it in a variable
gvn_fstnum = 10.56
# Give the second number as static input and store it in another variable
gvn_scndnum = -125.721

# Truncate the given first number to 0 decimal places or an integer using
# the trunc() function of the math module and print the result
print(math.trunc(gvn_fstnum))
# Similarly, truncate the given second number
print(math.trunc(gvn_scndnum))

Output:

10
-125

2)Using int() function:

Truncate decimal python: The built-in int() function converts a float to an integer, thereby truncating a float number by removing its decimal points.

The int() function is not the same as the round() and floor() functions. The function just removes anything that comes after the decimal, regardless of what comes before it. This differs from the round() function in that it does not round up. It is also distinct from floor(), which returns an integer that is now greater than a number.

Example:

# Give the first number as static input and store it in a variable
gvn_fstnum = 22.56
# Give the second number as static input and store it in another variable
gvn_scndnum = -350.721

# Truncate the given first number to 0 decimal places or an integer using
# the int() function and print the result
print(int(gvn_fstnum))
# Similarly, truncate the given second number using the int() function
print(int(gvn_scndnum))

Output:

22
-350

3)Using string methods:

Truncate(number python): In Python, string methods are also used to truncate a float. So we convert the float to string first, and then remove the decimal places. We can use str. split() to split the string and extract only the first split’s part.

Example:

# Give the first number as static input and store it in a variable
gvn_fstnum = 22.56
# Give the second number as static input and store it in another variable
gvn_scndnum = -350.721

# Convert the given first number to string, split into two parts i.e, before and 
# after the decimal point and print the first(Integer) part of it to get a truncated number
print(str(gvn_fstnum).split('.')[0])
# Similarly, do for the given second number
print(str(gvn_scndnum).split('.')[0])

Output:

22
-350

4)Using For Loop:

# Give the list of floats as static input and store it in a variable
gvn_lst = [10.234, -16.6272, 5.5, -9.05]
# Take a variable and initialize it with a empty list which contains the tuncated values
trunc_lst = []
# Iterate in the given list using the for loop
for numbr in gvn_lst:
    # Convert eah element of the given list to an integer using the int() function
    # and append it to the above declared new list using the append() function
    trunc_lst.append(int(numbr))
# Print the truncated values of the given list of float numbers
print(trunc_lst)

Output:

[10, -16, 5, -9]

5)Using f-strings:

Python truncate integer: You may want to truncate a float to a specific number of decimal points on occasion. This can be useful when displaying currencies or percentages, for example.

We can simply accomplish this by utilizing Python’s handy f-string features. We may use this to indicate the number of decimal points to retain and convert the string back to a float.

Example:

# Give the first number as static input and store it in a variable
gvn_fstnum = 22.568
# Give the second number as static input and store it in another variable
gvn_scndnum = -350.721
# Truncate the given first and second numbers upto 2 decimal places using f-strings
# and print the result
print(float(f'{gvn_fstnum:.2f}'))
print(float(f'{gvn_scndnum:.2f}'))

Output:

22.57
-350.72