今天发现两个float数据相减出现精度丢失问题
float newsw = 150f;
float oldsw = 154.4f;
float dv = oldsw - newsw;
System.out.println(dv);
正常情况下应该输出4.4,结果却输出4.399994,经研究可以改成以下代码解决
float newsw = 150f;
float oldsw = 154.4f;
BigDecimal newbd = new BigDecimal(String.valueOf(newsw));
BigDecimal oldbd = new BigDecimal(String.valueOf(oldsw));
float dv = oldbd.subtract(newbd).floatValue();
System.out.println(dv);