Float precision with specific numbers

前端 未结 3 902
逝去的感伤
逝去的感伤 2021-01-26 05:31

The following value gives me wrong precision. It is observed with only specific numbers. It might be a floating representation problem, but wanted to know the specific reason.

3条回答
  •  无人共我
    2021-01-26 06:20

    If you want decimal numbers to come out as exactly as you entered them in Java, use BigDecimal instead of float.

    Floating point numbers are inherently inaccurate for decimals because many numbers that terminate in decimal (e.g. 0.1) are recurring numbers in binary and floating point numbers are stored as a binary representation.

提交回复
热议问题