0

I am new to Python. I have two simple questions. In my code, I need to do something like the following:

50-49.9==0.1

but python is giving False. How can I correct this? Also, a similar wrong result I am getting is in the following:

int(10*(1-0.9))

I want the answer to be 1 but I am getting 0.

DrMittal
  • 7
  • 5

1 Answers1

0

In summary, the answer to your questions.

  1. 50 - 49.9 == 0.1 will not equate to True as the left hand side is not rounded to one decimal place.

  2. int(10*(1-0.9)) will result in int(10*0) as the equation inside an int statement always truncates towards zero. More details are in https://docs.python.org/3/library/functions.html#int

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • Number 2 is not quite correct. The problem is that 1-0.9 evaluates to 0.09999999999999998. Multiply by 10, and the value is below 1. Converting to int will remove the fraction, giving 0 – Stefan Aug 06 '20 at 14:49
  • ... if you multiply with 100, instead of 10, it will give 9, not 0. – Stefan Aug 06 '20 at 14:52