Jul-15-2021, 08:15 AM
some_value = 1.337 guessed_value = 1.3 # round(value, decimal_places) if round(some_value, 1) == round(guessed_value, 1): print("Rounded values are equal")If you made a calculation and want to compare if the two values are equal, you should use
math.isclose
.import math value_a = 1337 * 1.3 / 42 value_b = 1337 / 42 * 1.3 print("Value_a:", value_a) print("Value_b:", value_b) if value_a != value_b: print("The values are not equal") if math.isclose(value_a, value_b): print("But the values are close enough")
Quote:Value_a: 41.38333333333334
Value_b: 41.38333333333333
The values are not equal
But the values are close enough
Just changing the order of calculation, will result in a different value.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!