Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the number of digits after the decimal point

I'm trying to write a Python 2.5.4 code to write a function that takes a floating-point number x as input and returns the number of digits after the decimal point in x.

Here's my code:

def number_of_digits_post_decimal(x):
    count = 0
    residue = x -int(x)
    if residue != 0:
        multiplier = 1
        while int(multiplier * residue) != (multiplier * residue):
            count += 1
            multiplier = 10 * multiplier
            print count
            print multiplier
            print multiplier * residue
            print int(multiplier * residue)
            return count

print number_of_digits_post_decimal(3.14159)

The print statements within the while loop are only for debugging purposes.

Now when I run this code, I get the following as output.

1

10

1.4159

1

2

100

14.159

14

3

1000

141.59

141

4

10000

1415.9

1415

5

100000

14159.0

14158

6

1000000

141590.0

141589

7

10000000

1415900.0

1415899

8

100000000

14159000.0

14158999

9

1000000000

....

The final value of count as returned by this function is 17.

How to modify this code in order to achieve our desired result?

like image 437
Saaqib Mahmood Avatar asked Feb 23 '16 19:02

Saaqib Mahmood


People also ask

How do you find the number of digits after a decimal point?

The first digit after the decimal represents the tenths place. The next digit after the decimal represents the hundredths place. The remaining digits continue to fill in the place values until there are no digits left.

How do you find the number of digits in a decimal number?

decimal system, also called Hindu-Arabic number system or Arabic number system, in mathematics, positional numeral system employing 10 as the base and requiring 10 different numerals, the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. It also requires a dot (decimal point) to represent decimal fractions.


2 Answers

Here's a shortcut that you might like:

def num_after_point(x):
    s = str(x)
    if not '.' in s:
        return 0
    return len(s) - s.index('.') - 1
like image 193
DevShark Avatar answered Oct 27 '22 00:10

DevShark


This was interesting! So if you run the following:

x = 3.14159  
residue = x - int(x)  
print residue  

You will get the following result:

0.14158999999999988

This decimal does in fact have 17 digits. The only way that I found to override this was to avoid doing the subtraction (which is the root cause of the error, as you can see from the inaccuracy here). So this code should work as you expect:

def number_of_digits_post_decimal(x):  
    count = 0  
    residue = x -int(x)  
    if residue != 0:  
        multiplier = 1  
        while not (x*multiplier).is_integer():  
            count += 1  
            multiplier = 10 * multiplier  
        return count

This will just shift the decimal to the right until python identifies it as an integer (it will do a rightward shift exactly the number of times you want too). Your code actually worked as you intended for it to, something unintended just happened during the subtraction process. Hope this helps!

like image 21
JPope2014 Avatar answered Oct 26 '22 23:10

JPope2014