-4

如何在 python 中打印一个简单操作的完整浮点结果?我的代码是:

if( int(array_Y[counter2]) == int(round(float(elem[0])))):
                if(int(round(float(elem[0]))) == 0):
                    negatiu_verdader += 1
                if(int(round(float(elem[0]))) == 1):
                    positiu_verdader += 1
                counter = counter + 1
            counter2 = counter2 + 1

        error =  float(1.0000- (1.0000 * counter / counter2))

        print "        ERROR!!!!!!!!!!!!!!!!!!!!!!!! :" + ("{0:.15f}".format(round(error,2)))

但错误总是:0.420000000000000或者0.230000000000000但我希望错误是:0.43233213213232

4

1 回答 1

3

您通过调用将错误四舍五入到小数点后两位round(error, 2)

>>> round(0.43233213213232, 2)
0.43

如果您想显示更精确,请不要这样做:

>>> format(round(0.43233213213232, 2), '.15f')
'0.430000000000000'
>>> format(0.43233213213232, '.15f')
'0.432332132132320'

您在代码中做了很多多余的工作,稍微简化一下:

elem_rounded = int(round(float(elem[0])))
if int(array_Y[counter2]) == elem_rounded:
    if not elem_rounded:
        negatiu_verdader += 1
    elif elem_rounded == 1:
        positiu_verdader += 1
        counter += 1
        counter2 += 1

    error =  1.0 - (1.0 * counter / counter2)
    print "        ERROR!!!!!!!!!!!!!!!!!!!!!!!! :{0:.15f}".format(error)
于 2013-07-26T10:36:00.993 回答