Count Decimal Places in Python

How to count decimal places in Python?

In Python, we can represent decimal values using the float datatype. The decimal point divides the integer from the fractional part. The maximum value a float object can have is 1.8 x 10^308.

In this article, we will discuss how to count the decimal places in Python. We will take a float value and return the total number of digits after the decimal point.

Using the decimal library to count decimal places in Python

The decimal library allows us to work with float values. We can convert these values to objects of the Decimal class from this library. There are many methods associated with this class that can perform various functions on these values.

The as_tuple() function is used to return the information about the floating value in a tuple. It returns the sign of the number (positive or negative), the digits in the number, and the exponent value.

The exponent attribute can be used to return the exponent value from this tuple. This exponent value is the same as the number of digits in the decimal place with a negative sign. We can use the abs() function to get the absolute value of this attribute, removing the negative sign.

See the code below.

Output:

3

In the above example, we use the decimal.Decimal() constructor to create an object of the Decimal class. We get the exponent value of this object using the as_tuple() function. Its absolute value is displayed using the abs() function.

Using the len() and split() function to count decimal places in Python

The len() and split() function work with string objects. The former is used to return the length of the given string and the latter can split a given string into a list of substrings based on some character.

We can use these functions together to count decimal places in Python.

First, we will convert the float value to a string using the str() function. Then we will split the list based on the . character to return two substrings. The second substring contains the decimal part and its length can be calculated using the len() function.

For example,

Output:

3

Using the find() function to count decimal places in Python

The find() function is used to return the index of the first occurrence of an element in a string. We can use it with string slicing to count decimal places in Python.

We can slice string using the square brackets ([]). In these brackets, we can specify the start, end, and step values for the string slicing. We can specify the step as -1 to reverse the string. We can convert a float value to a string using the str() function and reverse it using this method.

After reversing the string, the decimal part comes in front. We can find the occurrence of the decimal point using the find() function. The index of the decimal point will be the same as the total decimal places.

This logic is implemented in the code below.

Output:

3

Conclusion

To conclude, we discussed several methods to count decimal places in Python. In our first method, we used the Decimal class from the decimal library. We can use the as_tuple() function to get the exponent value from the objects of this class that represent the decimal places.

The other two methods involved converting the float value to a string. In the first method, we split the string into the real and fractional parts using the split() function and found the decimal places using the len() function on the fractional part.

In the final method, we reversed the number using string slicing and found the occurrence of the decimal point which corresponds to the total decimal places.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 23 September

    Convert 0 to 1 and 1 to 0 in Python

    Table of ContentsWays to convert 0 to 1 and 1 to 0 in PythonUsing SubtractionUsing XORUsing not operatorUsing lambda expressionUsing addition and remainder operatorConvert 1 to 0 and 0 to 1 in listConvert 1 to 0 and 0 to 1 in numpy arrayUsing where with bitwise xorUsing bitwise operators In this post, we will see […]

  • 23 September

    TypeError: Object of Type Datetime Is Not Json Serializable in Python

    Table of ContentsFix the object of type datetime is not JSON serializable exception in PythonUsing the default parameter in the json.dumps() functionUsing the cls parameter in the json.dumps() functionUsing the str functionConclusion In Python, the datetime library allows us to create objects of the datetime class that can store and process date and time values […]

  • 23 September

    Add Tuple to List in Python

    Table of ContentsAdd tuple to list in PythonWays to Add Tuple to List in Python (adding the Tuple as An Element)Using the insert() Function to Add Tuple to List in PythonUsing the append() Function to Add Tuple to List in PythonWays to Add Tuple to List in Python (adding the Tuple’s Elements to A List)Using […]

  • 23 September

    Convert Datetime to Unix Timestamp in Python

    Table of ContentsDates and Unix TimestampWays to convert datetime to Unix timestampUsing the total_seconds() function to convert datetime to Unix timestamp in PythonUsing the timestamp() function to convert datetime to Unix timestamp in PythonUsing the strftime() function to convert datetime to Unix timestamp in PythonUsing the time.mktime() function to convert datetime to Unix timestamp in […]

  • 23 September

    Find All Substrings of String in Python

    Table of ContentsHow To Find All Substrings of String in Python?Using a User-Defined Function that Makes Use of String Slicing to Find All Substrings of String in Python.Using List Comprehension Along with String Slicing to Find All Substrings of String in Python.Using the itertools.Combinations() Function to Find All Substrings of String in Python. Dealing with […]

  • 21 September

    Print Bytes without b in Python

    Table of ContentsWays to Print Bytes without b in PythonUsing the decode() Function to Print Bytes without b in PythonUsing the str() Function to Print Bytes without b in PythonUsing the repr() Function to Print Bytes without b in PythonConclusion Just as a string is a collection of Unicode characters in Python 3, bytes are […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.