Truncate float in Python

Truncate float in Python

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.

The above code provides the following output:

1.26

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 the field_name, and the desired number places for truncating in place of the precision 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.

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.

The above code provides the following output:

1.25
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.

The above code provides the following output:

1.25
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.

The above code provides the following output:

1.26

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.

Related Posts

  • Python check for null
    17 November

    How to check for null in Python

    Table of ContentsHow to check if a variable is NULL in Python?Using the is operator to check if a variable is NULL in Python.Using the if condition to check if a variable is NULL in Python.Using the not equal to operator to check if a variable is NULL in Python. This article demonstrates the different […]

  • Split String with multiple delimiters
    17 November

    Split String With Multiple Delimiters In Python

    Table of ContentsSplitting a string in PythonWays to split a string in PythonUsing the split() and replace() functionsUsing the regular expressions In this post, we will see how to split String with multiple delimiters in Python. Splitting a string in Python When we talk about splitting a string, it means creating a collection of sub-strings […]

  • Funny Kahoot names
    17 November

    600+ Funny, Dirty, Inappropriate Names for Kahoot

    Table of ContentsFunny Names for KahootKahoot: A Brief OverviewHow to Change your Name on Kahoot?Funny Names for Kahoot: Overall EditionFunny Kahoot Names: Girls EditionFunny Names For Kahoot: Boys EditionFunny Names for Kahoot: Unique EditionFunny Troll Names for KahootFunny Inappropriate Names: Dirty Kahoot namesGood Kahoot NamesFunny Inappropriate Names: Set 2Frequently Asked Questions About Funny Names for […]

  • Funny steam names
    17 November

    600+ Funny Steam Names to Use in 2021

    Table of ContentsFunny Steam Names: Let’s Enhance your Steam Profile by Giving it a New NameAssigning Funny Steam Names:Step-By-Step Guide200+ Funny Steam Names70+ Cool Steam Names90 Good Steam Names50 Clever Steam Names30 Funny Steam Names For Boys35 Funny Steam Names For Girls25 Creative Cool Steam Names100+ Other Funny Steam Names to Consider in 2021Deleting Previous […]

  • Sort dictionary by value in Python
    17 November

    Sort dictionary by value in python

    Table of ContentsHow to sort sort dictionary by value in Python?Using for loop along with the sorted() function.Using the sorted() function to sort dictionary by value in Python.Using operator.itemgetter() function to sort sort dictionary by value in Python.Using lambda function to sort sort dictionary by value in Python.Using the OrderedDict() method to sort sort dictionary […]

  • Get class name in Python
    17 November

    Get class name in Python

    Table of ContentsHow to get the attributes of an object?Get Class name in python using the __class__ attribute Get Class name in python using type() functionConclusion While working with objects in python, several times we need to get their class name. In this article, we will discuss several ways to get the class name of an […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.