1. 环境:涉及到原始代码是用float取参与for循环,配置是-1~1,递进是0.1
2. 结果在-1,-0.9到-0.8的时候变成-0.79999,然后后面就崩掉了,精度就乱了。
3.前期解决方案
vin=Float.parseFloat(String.format("%.3f",vin));
但是这个方案是可以解决部分功能但是不能完全解决,如果场景是-1~0,递进是0.1就不行了。
第二个解决方案
我是算出循环的次数
int stepCnt = (int) Math.ceil(Math.abs(start - stop) / Math.abs(step));
接着使用do while
do{
vin=Float.parseFloat(String.format("%.3f",vin));
vin=vin+step;
}while(stepCnt-->0);