计算房贷利率月供相关信息


    
    /* 贷款本金 */
    private final static double BaseMoney = 1000000;
    /* 贷款年利率 */
    private final static float MonthRate = 0.0539f/12;
    /* 贷款月份 */
    private final static int MonthLength = 12*30;
    /* 总利息 */
    private static double lixi = 0;

    public static void main(String[] args) {
        
        /* 验证还款,因为有分的误差,需要微调才能确认 */
        double yueHuan = calyueHuan();
        BigDecimal bd = new BigDecimal(yueHuan);
        yueHuan = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();        

        yueHuan = getFinalResult(yueHuan, 0);
        System.out.println("最终月供:"+yueHuan);
        getHuankuanResult(yueHuan, true);
    }
    
    private static double getFinalResult(double yueHuan, double lastResult) {
        double result = getHuankuanResult(yueHuan, false);
        if (result < 0) {
            if (lastResult > 0) {
                /* 上次是少还,这次是多还,则为此值 */
                System.out.println("上次是少还,这次是多还,则为此值 yueHuan:"+yueHuan);
                return yueHuan;
            } else if (lastResult < 0) {
                /* 上次是多还,这次还是多还,继续减 */
                System.out.println("上次是多还,这次还是多还,继续减 yueHuan:"+yueHuan);
                yueHuan -= 0.01;
                return getFinalResult(yueHuan, result);
            } else {
                /* 多还了 */
                System.out.println("多还了 yueHuan:"+yueHuan);
                yueHuan -= 0.01;
                return getFinalResult(yueHuan, result);
            }
        } else if (result > 0) {
            if (lastResult > 0) {
                /* 上次是少还,这次还是少还,继续加 */
                System.out.println("上次是少还,这次还是少还,继续加 yueHuan:"+yueHuan);
                yueHuan += 0.01;
                return getFinalResult(yueHuan, result);
            } else if (lastResult < 0) {
                /* 上次是多还,这次是少还了,则就是上次 */
                System.out.println("上次是多还,这次是少还了,则就是上次 yueHuan:"+yueHuan);
                return yueHuan + 0.01;
            } else {
                /* 少还了 */
                System.out.println("少还了 yueHuan:"+yueHuan);
                yueHuan += 0.01;
                return getFinalResult(yueHuan, result);
            }
        }
        System.out.println("刚好是0 yueHuan:"+yueHuan);
        return yueHuan;
    }

    /* 计算月供 */
    private static double calyueHuan() {
        double result = Math.pow(MonthRate+1, MonthLength)*BaseMoney;
        double result2 = 0;
        for (int i = 0; i < MonthLength; i++) {
            result2 += Math.pow(MonthRate+1,i);
        }
        
        double yueHuann = result/result2;
        System.out.println("月供毛坯值:"+yueHuann);
        return yueHuann;
    }
    
    /* 传入月供计算所有月份结束之后剩余本息和 */
    private static double getHuankuanResult(double yueHuann, boolean isPrintLog) {
        double tmpBaseMoney = BaseMoney;
        for (int i = 0; i < MonthLength; i++) {
            double monthLixi = tmpBaseMoney*MonthRate;
            tmpBaseMoney += monthLixi;
            double benxi = tmpBaseMoney;
            tmpBaseMoney -= yueHuann;
            lixi += monthLixi;
            
            if (isPrintLog) {
                System.out.println("第"+(i+1)+"个月"+" 你欠债本息共:"+benxi+" 还了"+yueHuann+" 剩余欠债本息:"+tmpBaseMoney+" 其中月利息:"+monthLixi+" 本月还本金:"+(yueHuann-monthLixi)+" 总利息:"+lixi);
            }
        }
        
        return tmpBaseMoney;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章