Equal decimal numbers in Python
This question already has an answer here:
The answer here is that you should not represent indivisible values as floats. Represent everything in cents.
The reason that is the solution is that floats cannot precisely represent all decimals. Accordingly, division by different amounts will not do what you want. Instead, you need to divide integral amounts, and work appropriately with any remainders that occur.
In general, equality checks on floats are also dangerous for that reason: you instead need to decide what difference is "equal enough" and test for that.
Multiply everything by 100 to get rid of the decimals. So a dime should be ten cents, a dollar 100 cents and so on. Since floats aren't perfect representations of a number you shoudn't do any equal checkes on them or doubles.
链接地址: http://www.djcxy.com/p/85810.html上一篇: 数字格式
下一篇: Python中的相等十进制数