计算Python中小数点后的位数

时间:2019-11-04 07:17:58

标签: python floating-point

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)

结果: 小数点后的位数= 46 应该是3!

0 个答案:

没有答案