What's the right way to parseFloat in Java

后端 未结 7 1754
[愿得一人]
[愿得一人] 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:22

    See What Every Computer Scientist Should Know About Floating-Point Arithmetic. Your results look correct to me.

    If you don't like how floating-point numbers work, try something like BigDecimal instead.

提交回复
热议问题