1
>>> 1.4 / 7
0.19999999999999998

Why??

I would like to get 0.2, so the issue is not the Python2 to Python3 int/float division change. Is there any solution other than using Decimal?

Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AM D64)] on win32

rezzy
  • 41
  • 2
  • 9

2 Answers2

1

Because 1.4 is not represented as 1.4 but as 1.39999999999999991118 in double precision floating point arithmetic (with 20 digits output precision). You can either use the Decimal type or round when you format the output ("%.1f" % 1.4 vs. "%.20f" % 1.4)

W.Mann
  • 893
  • 5
  • 11
0

You can use round(1.4/7,10) I will suggest check the documentation https://docs.python.org/3/tutorial/floatingpoint.html

Sourav De
  • 483
  • 4
  • 16
  • That won't change the problem. `0.2`is not decimal number 0.2 but 0.20000000000000001110 (rounded to 20 digits) in double precision floating point arithmetic. – W.Mann Jun 28 '17 at 10:58
  • I am unable to understand the problem here this is binary to decimal representation change the type if you are not comfortable with float type data. – Sourav De Jun 28 '17 at 11:01