Fix float precision with decimal numbers

后端 未结 4 1406
盖世英雄少女心
盖世英雄少女心 2021-01-22 16:49
a = 1

for x in range(5):
    a += 0.1
    print(a)

This is the result:

1.1
1.2000000000000002
1.3000000000000003
1.4000000000000004
1.         


        
4条回答
  •  执念已碎
    2021-01-22 17:19

    You could format your output like this;

    a=1
    for x in range(5):
        a += 0.1
        print("{:.9f}".format(a) )
    

提交回复
热议问题