Remove Decimal Places in Python

💡 Quick Solution

To Remove decimal places in Python, use int() function.

Output

3

Two of the most common types to represent numeric values are int and float. The int type is used to represent a certain range of integers. The float type represents values with fractional parts. The latter requires more memory and also represents a wider range than the int type.

Remove Decimal Places in Python

In this article, we will discuss how to remove decimal places in Python. For this, we will convert the float values to an integer.

Using the int() function

The int() function is used to typecast a value to an integer. To remove decimal places in Python we will pass a float value with a decimal part to this function and it will remove decimal places in Python.

For example,

Output

2

In the above example, we convert the float value to an integer and the decimal places are discarded.

Using the math library

The math library is used in Python to provide functions and constants that can calculate complex mathematical calculations. We can remove decimal places in Python using the truncate() function from the math library.

This function takes a float value and removes the decimal part from the same, returning the integer value.

See the code below.

Output:

2

We can also use the floor() and ceil() functions from this library. The former function returns the largest integer smaller than the given number and the latter returns the smallest integer greater than the provided value.

It will round off the float value and return the calculated integer value.

For example,

2
3

Using string formatting

String formatting refers to the technique of getting the output string in our desired format. Using the %d specifier we can get the output as an integer.

There are three ways in which we can use this format. The first method is by using the % formatting where we specify the format along with the variable.

See the code below.

Output:

2

Using the round() function

We can also use the round() function order to round the float value to the required integer and remove decimal places in Python.

See the code below.

3

Please note that round() will remove decimal places, but it will also round the number to nearest integer.

Conclusion

To conclude this tutorial, we discussed several methods to remove decimal places in Python. For this, we converted the float type values to an integer using several methods. In the first method, we used the int function to typecast the float value. Then we used the round() function which rounded off the given value to the nearest integer.

We also demonstrated the use of functions from the math library. These were the trunc(), floor(), and ceil() functions. In the final method, we discussed how to use string formatting to remove decimal places in Python.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 27 December

    Create Array of All NaN Values in Python

    Table of ContentsUsing numpy.empty() FunctionUsing numpy.full() FunctionUsing numpy.tile() FunctionUsing numpy.repeat() FunctionUsing Multiplication of numpy.ones() with nan Using numpy.empty() Function To create an array of all NaN values in Python: Use numpy.empty() to get an array of the given shape. Assign numpy.nan to every array element using the assignment operator (=). [crayon-63da57b26cbf3415805028/] [crayon-63da57b26cbf9008090322/] We used numpy.empty() […]

  • 27 December

    Call Function from Another Function in Python

    Table of ContentsCall a Function in PythonCall Function from Another Function in PythonCall a Function from Another Function within the Same/Different Classes Call a Function in Python To call a function in Python: Write a test() function, which prints a message. Call the function defined in the previous step. [crayon-63da57b26ce02547337428/] [crayon-63da57b26ce06211584827/] To call a function, […]

  • 27 December

    [Fixed] NameError Name ‘unicode’ is Not Defined in Python

    Table of ContentsUse str() MethodUse sys.version_info with strUse six.text_type Use str() Method To resolve the NameError: name 'unicode' is not defined, replace the occurrence of unicode() with str(). This issue occurs because unicode() was renamed to str() in Python 3. [crayon-63da57b26cf54549254025/] [crayon-63da57b26cf57638323038/] Unicode is a computing industry standard that ensures that text from most of […]

  • 26 December

    Repeat List N Times in Python

    Table of ContentsUsing the * operatorUsing the numpy.repeat() functionUsing the list comprehension techniqueUsing the itertools.repeat() functionConclusion This tutorial will demonstrate how to repeat list n times in Python. Using the * operator To repeat list n times in Python, use the * operator. Star operator(*) is used to multiply list by number e.g. lst*3 and […]

  • 26 December

    Get First Day of Next Month in Python

    Table of ContentsGet First Day of Next Month in PythonUsing the datetime.replace() with datetime.timedelta() functionUsing the calendar.monthrange() functionUsing the dateutil.relativedelta objectConclusion Get First Day of Next Month in Python This tutorial will demonstrate how to get first day of next month in Python. Using the datetime.replace() with datetime.timedelta() function To get first day of next […]

  • 25 December

    Convert String Array to Int Array in Python

    Table of ContentsUsing the for loop with int() functionUsing for loop with eval() functionUsing the map() with list() functionConclusion This tutorial will demonstrate how to convert string array to int array in Python. Using the for loop with int() function To convert string array to int array in Python: Use the for loop to loop […]

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.