Skip to main content

How to Count Decimal Places in Python

Determining the number of digits after the decimal point in a floating-point number is a common task.

This guide explores various methods to count decimal places in Python using the Decimal class and string manipulation techniques, providing accurate and reliable results.

Counting Decimal Places with Decimal

The Decimal class from the decimal module provides precise decimal arithmetic and makes it straightforward to determine the number of decimal places.

from decimal import Decimal

my_decimal = Decimal('3.14567')
count_after_decimal = abs(my_decimal.as_tuple().exponent)
print(count_after_decimal) # Output: 5
  • First we create the decimal object.
  • Then the abs(my_decimal.as_tuple().exponent) method returns the exponent of the decimal, representing the number of decimal places.
  • The abs() is needed to ensure a positive value is returned as the exponent is usually negative.
note

The Decimal class is recommended for precise decimal calculations, especially when dealing with financial or scientific data, where rounding errors can be significant. Using Decimal ensures more accurate tracking of decimal places.

Counting Decimal Places with String Manipulation (split('.'))

You can convert the float to a string and use the .split('.') method to get a list of whole and fractional parts:

my_float = 3.14567
count_after_decimal = len(str(my_float).split('.')[1])
print(count_after_decimal) # Output: 5
  • The str(my_float) converts the float to a string.
  • .split('.') splits the string into two parts at the decimal point.
  • The method len() then measures the length of the part after the decimal.
  • This method is simple, but might run into some problems with numbers that can't be stored exactly, like with using Decimal you can have predictable results and exact preservation of the decimal digits.

Counting Decimal Places with String Manipulation (re.sub())

You can also use a regular expression to remove everything but the digits after the decimal:

import re

my_float = 3.14567
count_after_decimal = len(re.sub(r'^\d+\.', '', str(my_float)))
print(count_after_decimal) # Output: 5
  • The regex r'^\d+\.' matches one or more digits (\d+) at the start of the string (^) followed by a decimal point (\.).
  • Then, re.sub replaces everything that matches, with an empty string.
  • This leaves only the part after the decimal point, allowing len() to be used to determine the decimal count.
  • This approach is useful in some cases, however it is recommended to use Decimal instead.

Counting Decimal Places in Standard Floats with String Reversal and .find()

This method uses the find() and slicing operators together, but is not recommended as it can not be always relied on.

my_float = 3.14567
count_after_decimal = str(my_float)[::-1].find('.')
print(count_after_decimal) # Output: 5
  • The [::-1] slice reverses the string.
  • str.find('.') finds the position of the period character.
  • Because strings can not store some floating point values with absolute accuracy, the results may be unpredictable.