What's the right way to parseFloat in Java

后端 未结 7 1756
[愿得一人]
[愿得一人] 2021-01-12 01:52

I notice some issues with the Java float precision

       Float.parseFloat(\"0.0065\") - 0.001  // 0.0055000000134110451
       new Float(\"0.027\") - 0.001          


        
相关标签:
7条回答
  • 2021-01-12 02:33

    Long story short if you require arbitrary precision use BigDecimal not float or double. You will see all sorts of rounding issues of this nature using float.

    As an aside be very careful not to use the float/double constructor of BigDecimal because it will have the same issue. Use the String constructor instead.

    0 讨论(0)
提交回复
热议问题