double是一个不准确的类型  用BigDecimal类类似代码有public class While1{
    public static void main(String[] args){
        double a=3.0;
        double b=2.9;
        System.out.println(a-b);
    }
    
}
输出结果0.10000000000000009

解决方案 »

  1.   

    /*第一种方法*/
    import java.math.BigDecimal;
    class while2{
        public static void main(String[] args){
            //总共的钱,每天存的钱,每五天用点钱
            。。
            while(true){
                。
                if(day%5==0){
                sum-=withdraw;
                }
    BigDecimal bd = new BigDecimal(sum);
    bd = bd.setScale(2,BigDecimal.ROUND_HALF_UP);//精确到小数点后两位,四舍五入
                System.out.println(bd+"---"+day);
            }
            。
        }
    }
    /*第二种方法*/
    import java.text.NumberFormat;
    class while2{
        public static void main(String[] args){
            。
            while(true){
                。。
    NumberFormat nfm = NumberFormat.getInstance();
    nfm.setMaximumFractionDigits(2);//设置数值的小数部分允许的最大位数
    nfm.setMinimumFractionDigits(2);//设置数值的小数部分允许的最小位数
                System.out.println(nfm.format(sum)+"---"+day);
            }
           。。
        }
    }