+ 2

[python] multiplication in for loop

My code is like this: for i in range(10): print(i*0.1) And output is: 0.0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6000000000000001 0.7000000000000001 0.8 0.9 why output is not 0.1, 0.2, 0.3, 0.4......??

12th Feb 2018, 9:36 AM
Yen Chai
Yen Chai - avatar
1 ответ
+ 1
you can round it to one decimal for i in range(10): a = i*0.1 print("%.1f" % a)
12th Feb 2018, 10:24 AM
Toni Isotalo
Toni Isotalo - avatar