0

Im trying to make a function that returns the amount of digits pass the decimal point I dont know what to do.

So for example

  • 5.67 returns 2 digits
  • 5.159 returns 3 digits
  • 5.000 returns 0 digits

I have tried to use the round() function but that doesnt work

Any suggestions?

My code so far enter image description here

1 Answers1

0

how is this? Please let me know :)

def get_num_digits_after_point(num):
    newNum = str(num)

    should_I_count = False
    count = 0
    for char in newNum:
        if char == '.':
            should_I_count = True
        elif should_I_count:
            count += 1

    q = False
    NEWcount = 0
    d = 0
    for i in newNum:
        if i == '.':
            q = True
        elif q:
            d += 1
            if i == '0':
                NEWcount += 1
    # print(NEWcount)
    # print(d)
    if NEWcount == d:
        return 0
    else:
        return count


print(get_num_digits_after_point(5.67))
print(get_num_digits_after_point(5.159))
print(get_num_digits_after_point(5.001))
print(get_num_digits_after_point(5.000))
Tkinter Lover
  • 835
  • 3
  • 19