0
def ndigits(n):
    y=n-int(n)
    print(y)
    #y = 0.45600000000000307 ! it should be 0.456

    after=0
    while(y!=0):
        #print(y)
        y=y*10
        y=y-int(y)
        after +=1
    print("number of digits after decimal point=",after)


ndigits(123.456)

Result: the number of digits after decimal point= 46 it should be 3!

ayat ullah sony
  • 1,973
  • 1
  • 10
  • 7
  • def ndigits(n): y = str(n) print("number of digits after decimal point = ", len(y.split('.')[1])) ndigits(123.456) – Z. Cajurao Nov 04 '19 at 07:51
  • Just replace `while (y != 0)` by `while (y > 1e-10)`. Before the while-loop, you should add 1e-11 to n, to also capture the case when y would be represented as 123.455999999999999 – JohanC Nov 04 '19 at 07:59
  • Note that floating point math has about 15 decimal digits of precision, but is internally represented in binary – JohanC Nov 04 '19 at 08:01

0 Answers0