Signup/Sign In

The 'trunc' function in Python

Posted in Programming   LAST UPDATED: OCTOBER 7, 2021

    We have seen many functions from the python math module in our previous posts. In this post, we will look at one of the prominent functions of the math module which is the trunc function.

    The function trunc basically refers to truncate. It helps in truncating the value of any integer/decimal number which is passed as an argument to it. It can be said that this function provides a simple way of rounding off a number. Every digit after the decimal is replaced by 0, and hence the output is a number without decimals.

    Before using the trunc function, one has to import the math module and use the trunc method by referencing the math module using the dot operator like math.trunc(val).

    Features of trunc function

    1. Integer, as well as float values, can be passed as arguments to the trunc function.

    2. It returns an integer value that is the integer part of the value which is passed as an argument.

    What if the user passes a string or some value other than an integer or floating-point value?

    Simple. It gracefully throws an error, which has been demonstrated below:

    import math
    print(math.trunc("Studytonight"))

    Output:

    TypeError: type str doesn't define __trunc__ method
    


    Syntax of trunc function

    The below code demonstrates the syntax of the trunc function and how to use it,

    import math
    
    print(trunc(value))

    Now let's cover a few examples to see how this function works.


    1. Python trunc function with positive integers

    It simply returns the positive integer which is passed to the trunc function because there is no decimal part that needs to be truncated from it.

    Let's see a code example:

    import math
    
    num = 12
    print(math.trunc(num))

    Output:

    12
    


    2. Python trunc function with negative integers

    This behaves in the same way as the previous example. The negative number along with its symbol is retained and returned back as output.

    Let's see a code example:

    import math
    
    num = -12
    print(math.trunc(num))

    Output:

    -12
    


    3. Python trunc function with positive floating-point numbers

    When a positive floating-point number is passed to this function, it truncates the decimal part of the number and returns it.

    Let's see a code example:

    import math
    
    num = 12.98
    print(math.trunc(num))

    Output:

    12
    


    4. Python trunc function with negative floating-point numbers

    When a negative floating-point number is passed to this function as an argument, it retains the negative sign but truncates the decimal part of the number.

    Let's see a code example:

    import math
    
    num = -12.03
    print(math.trunc(num))

    Output:

    -12
    


    5. Python trunc function with a list of values

    The numbers can be defined in a list and iterated over. It will truncate every item of the list and output back the result.

    Let's see a code example:

    import math
    
    num_list = [-12.03, 12.90, 12, -12]
    # iterating over the list
    for i in num_list:
        print(math.trunc(i))

    Output:

    -12
    12
    12
    -12


    6. Python trunc function with a tuple of values

    This works in the same way as a list of items. The tuple values are iterated over, and every item in the tuple is truncated.

    Let's see a code example:

    import math
    
    num_tuple = (-12.03, 12.90, 12, -12)
    for i in num_tuple:
        print(math.trunc(i))

    Output:

    -12
    12
    12
    -12

    In short, this function behaves like the ceil function for negative numbers. It also behaves like the floor function for positive numbers.

    Note: The Numpy module also has a truncate function which can be used using numpy.trunc. It behaves in the same way as trunc function of the math module but values are displayed slightly differently.


    Conclusion

    In this post, we understood the significance of the trunc method with the help of some examples. Do let us know how you would use this function and for what purpose.

    You may also like:

    About the author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    Tags:PythonPython Math Module
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS