Convert double to float in Java

前端 未结 10 832
执念已碎
执念已碎 2020-12-08 18:13

I am facing an issue related to converting double to float. Actually, I store a float type, 23423424666767, in a database, but when we

10条回答
  •  旧时难觅i
    2020-12-08 18:51

    First of all, the fact that the value in the database is a float does not mean that it also fits in a Java float. Float is short for floating point, and floating point types of various precisions exist. Java types float and double are both floating point types of different precision. In a database both are called FLOAT. Since double has a higher precision than float, it probably is a better idea not to cast your value to a float, because you might lose precision.

    You might also use BigDecimal, which represent an arbitrary-precision number.

提交回复
热议问题