point behaviour in a Java program
This question already has an answer here:
The most common storage for floating-point values in programming languages - IEEE singles and doubles - does not have exact representations for most decimal fractions.
The reason is that they store values in binary floating-point format, rather than decimal floating-point format. The only fractional values which can be represented exactly are those which are sums of negative powers of two. Numbers like:
Etc.
What you are seeing is the fact that representations of numbers like 0.96 are not exactly representable, because they are not expressible as a sum of negative powers of two. Thus, when printed out with full precision as a decimal fraction, they won't match the original value.
另请参阅“每位计算机科学家应了解的浮点数”
Other answers mentioned why, but not how to avoid it.
There are several solutions:
numeric
type in SQL), you can use it. 上一篇: 为什么2.9000000000000004而不是2.9?
下一篇: 在Java程序中的点行为