i use python3, the code below:
import math
print(round(1.755,2))
print(round(1.7555,3))
print("%.2f" % 1.755)
print("%.3f" % 1.7555)
the result:
1.75
1.756
1.75
1.756
why, does round(1.755,2) = 1.76
??
i use python3, the code below:
import math
print(round(1.755,2))
print(round(1.7555,3))
print("%.2f" % 1.755)
print("%.3f" % 1.7555)
the result:
1.75
1.756
1.75
1.756
why, does round(1.755,2) = 1.76
??
From the documentation for round
Note The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.
This isn't a problem with Python, it's just a property of floating point numbers. There is a certain degree of error in the mapping between floats and real numbers. This causes some rounding cases to be unpredictable.