Precision lost in float value using java

后端 未结 5 1117
醉酒成梦
醉酒成梦 2020-12-03 16:15

Given below the test code and its output. When I get float value from number value, precision is lost.. Can anyone tell me why this behaviour and also how to handle this?

5条回答
  •  庸人自扰
    2020-12-03 17:03

    All data types have representation limits so the fact there is a limit shouldn't be surprising.

    float uses 24-bit for its "mantissa" which holds all the significant digits. This means it has about 7 digits of precision (as 2^^24 is about 16 million)

    double uses 53-bit for it "mantissa" so it can hold about 16 digits accurately.

提交回复
热议问题