等額本息、等額本金 計算(java)

記錄一下

等額本息

package javatest;

/**
 * @author Kido
 * @email everlastxgb@gmail.com
 * @create_time 2016/8/9 18:32
 */
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 等額本息還款婆咸,也稱定期付息唉铜,即借款人每月按相等的金額償還貸款本息玖院,其中每月貸款利息按月初剩余貸款本金計算并逐月結清菠红。把按揭貸款的本金總額與利息總額相加,
 * 然后平均分攤到還款期限的每個月中难菌。作為還款人试溯,每個月還給銀行固定金額,但每月還款額中的本金比重逐月遞增郊酒、利息比重逐月遞減遇绞。
 */
public class AverageCapitalPlusInterestUtils {

    /**
     * 等額本息計算獲取還款方式為等額本息的每月償還本金和利息
     * <p>
     * 公式:每月償還本息=〔貸款本金×月利率×(1+月利率)^還款月數(shù)〕÷〔(1+月利率)^還款月數(shù)-1〕
     *
     * @param invest 總借款額(貸款本金)
     * @param yearRate 年利率
     * @param totalMonth 還款總月數(shù)
     * @return 每月償還本金和利息, 不四舍五入,直接截取小數(shù)點最后兩位
     */
    public static double getPerMonthPrincipalInterest(double invest, double yearRate, int totalMonth) {
        double monthRate = yearRate / 12;
        BigDecimal monthIncome = new BigDecimal(invest)
                .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalMonth)))
                .divide(new BigDecimal(Math.pow(1 + monthRate, totalMonth) - 1), 2, BigDecimal.ROUND_DOWN);
        return monthIncome.doubleValue();
    }

    /**
     * 等額本息計算獲取還款方式為等額本息的每月償還利息
     * <p>
     * 公式:每月償還利息=貸款本金×月利率×〔(1+月利率)^還款月數(shù)-(1+月利率)^(還款月序號-1)〕÷〔(1+月利率)^還款月數(shù)-1〕
     *
     * @param invest 總借款額(貸款本金)
     * @param yearRate 年利率
     * @param totalMonth 還款總月數(shù)
     * @return 每月償還利息
     */
    public static Map<Integer, BigDecimal> getPerMonthInterest(double invest, double yearRate, int totalMonth) {
        Map<Integer, BigDecimal> map = new HashMap<Integer, BigDecimal>();
        double monthRate = yearRate / 12;
        BigDecimal monthInterest;
        for (int i = 1; i < totalMonth + 1; i++) {
            BigDecimal multiply = new BigDecimal(invest).multiply(new BigDecimal(monthRate));
            BigDecimal sub = new BigDecimal(Math.pow(1 + monthRate, totalMonth)).subtract(new BigDecimal(Math.pow(1 + monthRate, i - 1)));
            monthInterest = multiply.multiply(sub).divide(new BigDecimal(Math.pow(1 + monthRate, totalMonth) - 1), 6, BigDecimal.ROUND_DOWN);
            monthInterest = monthInterest.setScale(2, BigDecimal.ROUND_DOWN);
            map.put(i, monthInterest);
        }
        return map;
    }

    /**
     * 等額本息計算獲取還款方式為等額本息的每月償還本金
     *
     * @param invest 總借款額(貸款本金)
     * @param yearRate 年利率
     * @param totalMonth 還款總月數(shù)
     * @return 每月償還本金
     */
    public static Map<Integer, BigDecimal> getPerMonthPrincipal(double invest, double yearRate, int totalMonth) {
        double monthRate = yearRate / 12;
        BigDecimal monthIncome = new BigDecimal(invest)
                .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalMonth)))
                .divide(new BigDecimal(Math.pow(1 + monthRate, totalMonth) - 1), 2, BigDecimal.ROUND_DOWN);
        Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalMonth);
        Map<Integer, BigDecimal> mapPrincipal = new HashMap<Integer, BigDecimal>();

        for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
            mapPrincipal.put(entry.getKey(), monthIncome.subtract(entry.getValue()));
        }
        return mapPrincipal;
    }

    /**
     * 等額本息計算獲取還款方式為等額本息的總利息
     *
     * @param invest 總借款額(貸款本金)
     * @param yearRate 年利率
     * @param totalMonth 還款總月數(shù)
     * @return 總利息
     */
    public static double getInterestCount(double invest, double yearRate, int totalMonth) {
        BigDecimal count = new BigDecimal(0);
        Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, totalMonth);

        for (Map.Entry<Integer, BigDecimal> entry : mapInterest.entrySet()) {
            count = count.add(entry.getValue());
        }
        return count.doubleValue();
    }

    /**
     * 應還本金總和
     *
     * @param invest 總借款額(貸款本金)
     * @param yearRate 年利率
     * @param totalMonth 還款總月數(shù)
     * @return 應還本金總和
     */
    public static double getPrincipalInterestCount(double invest, double yearRate, int totalMonth) {
        double monthRate = yearRate / 12;
        BigDecimal perMonthInterest = new BigDecimal(invest)
                .multiply(new BigDecimal(monthRate * Math.pow(1 + monthRate, totalMonth)))
                .divide(new BigDecimal(Math.pow(1 + monthRate, totalMonth) - 1), 2, BigDecimal.ROUND_DOWN);
        BigDecimal count = perMonthInterest.multiply(new BigDecimal(totalMonth));
        count = count.setScale(2, BigDecimal.ROUND_DOWN);
        return count.doubleValue();
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        double invest = 10000;//本金
        int month = 4;
        double yearRate = 0.12;//年利率
        double perMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
        System.out.println("等額本息---每月還款本息:" + perMonthPrincipalInterest);
        Map<Integer, BigDecimal> mapInterest = getPerMonthInterest(invest, yearRate, month);
        System.out.println("等額本息---每月還款利息:" + mapInterest);
        Map<Integer, BigDecimal> mapPrincipal = getPerMonthPrincipal(invest, yearRate, month);
        System.out.println("等額本息---每月還款本金:" + mapPrincipal);
        double count = getInterestCount(invest, yearRate, month);
        System.out.println("等額本息---總利息:" + count);
        double principalInterestCount = getPrincipalInterestCount(invest, yearRate, month);
        System.out.println("等額本息---應還本息總和:" + principalInterestCount);
    }
}

等額本金

package javatest;


/**
 * @author Kido
 * @email everlastxgb@gmail.com
 * @create_time 2016/8/9 18:36
 */

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 等額本金是指一種貸款的還款方式燎窘,是在還款期內(nèi)把貸款數(shù)總額等分摹闽,每月償還同等數(shù)額的本金和剩余貸款在該月所產(chǎn)生的利息,這樣由于每月的還款本金額固定褐健,
 * 而利息越來越少付鹿,借款人起初還款壓力較大,但是隨時間的推移每月還款數(shù)也越來越少蚜迅。
 */
public class AverageCapitalUtils {

    /**
     * 等額本金計算獲取還款方式為等額本金的每月償還本金和利息
     * <p>
     * 公式:每月償還本金=(貸款本金÷還款月數(shù))+(貸款本金-已歸還本金累計額)×月利率
     *
     * @param invest   總借款額(貸款本金)
     * @param yearRate 年利率
     * @param totalMonth    還款總月數(shù)
     * @return 每月償還本金和利息, 不四舍五入倘屹,直接截取小數(shù)點最后兩位
     */
    public static Map<Integer, Double> getPerMonthPrincipalInterest(double invest, double yearRate, int totalMonth) {
        Map<Integer, Double> map = new HashMap<Integer, Double>();
        // 每月本金
        double monthPri = getPerMonthPrincipal(invest, totalMonth);
        // 獲取月利率
        double monthRate = yearRate / 12;
        monthRate = new BigDecimal(monthRate).setScale(6, BigDecimal.ROUND_DOWN).doubleValue();
        for (int i = 1; i <= totalMonth; i++) {
            double monthRes = monthPri + (invest - monthPri * (i - 1)) * monthRate;
            monthRes = new BigDecimal(monthRes).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
            map.put(i, monthRes);
        }
        return map;
    }

    /**
     * 等額本金計算獲取還款方式為等額本金的每月償還利息
     * <p>
     * 公式:每月應還利息=剩余本金×月利率=(貸款本金-已歸還本金累計額)×月利率
     *
     * @param invest   總借款額(貸款本金)
     * @param yearRate 年利率
     * @return 每月償還利息
     */
    public static Map<Integer, Double> getPerMonthInterest(double invest, double yearRate, int totalMonth) {
        Map<Integer, Double> inMap = new HashMap<Integer, Double>();
        double principal = getPerMonthPrincipal(invest, totalMonth);
        Map<Integer, Double> map = getPerMonthPrincipalInterest(invest, yearRate, totalMonth);
        for (Map.Entry<Integer, Double> entry : map.entrySet()) {
            BigDecimal principalBigDecimal = new BigDecimal(principal);
            BigDecimal principalInterestBigDecimal = new BigDecimal(entry.getValue());
            BigDecimal interestBigDecimal = principalInterestBigDecimal.subtract(principalBigDecimal);
            interestBigDecimal = interestBigDecimal.setScale(2, BigDecimal.ROUND_DOWN);
            inMap.put(entry.getKey(), interestBigDecimal.doubleValue());
        }
        return inMap;
    }

    /**
     * 等額本金計算獲取還款方式為等額本金的每月償還本金
     * <p>
     * 公式:每月應還本金=貸款本金÷還款月數(shù)
     *
     * @param invest     總借款額(貸款本金)
     * @param totalMonth 還款總月數(shù)
     * @return 每月償還本金
     */
    public static double getPerMonthPrincipal(double invest, int totalMonth) {
        BigDecimal monthIncome = new BigDecimal(invest).divide(new BigDecimal(totalMonth), 2, BigDecimal.ROUND_DOWN);
        return monthIncome.doubleValue();
    }

    /**
     * 等額本金計算獲取還款方式為等額本金的總利息
     *
     * @param invest     總借款額(貸款本金)
     * @param yearRate   年利率
     * @param totalMonth 還款總月數(shù)
     * @return 總利息
     */
    public static double getInterestCount(double invest, double yearRate, int totalMonth) {
        BigDecimal count = new BigDecimal(0);
        Map<Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, totalMonth);

        for (Map.Entry<Integer, Double> entry : mapInterest.entrySet()) {
            count = count.add(new BigDecimal(entry.getValue()));
        }
        return count.doubleValue();
    }

//    /**
//     * @param args
//     */
//    public static void main(String[] args) {
//        double invest = 10000; // 本金
//        int month = 12;
//        double yearRate = 0.15; // 年利率
//        Map<Integer, Double> getPerMonthPrincipalInterest = getPerMonthPrincipalInterest(invest, yearRate, month);
//        System.out.println("等額本金---每月本息:" + getPerMonthPrincipalInterest);
//        double benjin = getPerMonthPrincipal(invest, month);
//        System.out.println("等額本金---每月本金:" + benjin);
//        Map<Integer, Double> mapInterest = getPerMonthInterest(invest, yearRate, month);
//        System.out.println("等額本金---每月利息:" + mapInterest);
//
//        double count = getInterestCount(invest, yearRate, month);
//        System.out.println("等額本金---總利息:" + count);
//    }
}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市慢叨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌务蝠,老刑警劉巖拍谐,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異馏段,居然都是意外死亡轩拨,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門院喜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來亡蓉,“玉大人,你說我怎么就攤上這事喷舀】潮簦” “怎么了淋肾?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長爸邢。 經(jīng)常有香客問我樊卓,道長,這世上最難降的妖魔是什么杠河? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任碌尔,我火速辦了婚禮,結果婚禮上券敌,老公的妹妹穿的比我還像新娘唾戚。我一直安慰自己,他們只是感情好待诅,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布叹坦。 她就那樣靜靜地躺著,像睡著了一般咱士。 火紅的嫁衣襯著肌膚如雪立由。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天序厉,我揣著相機與錄音锐膜,去河邊找鬼。 笑死弛房,一個胖子當著我的面吹牛道盏,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播文捶,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼荷逞,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了粹排?” 一聲冷哼從身側響起种远,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎顽耳,沒想到半個月后坠敷,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡射富,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年膝迎,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片胰耗。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡限次,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出柴灯,到底是詐尸還是另有隱情卖漫,我是刑警寧澤费尽,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站懊亡,受9級特大地震影響依啰,放射性物質發(fā)生泄漏。R本人自食惡果不足惜店枣,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一速警、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鸯两,春花似錦闷旧、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至钝侠,卻和暖如春该园,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背帅韧。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工里初, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人忽舟。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓双妨,卻偏偏與公主長得像,于是被迫代替她去往敵國和親叮阅。 傳聞我的和親對象是個殘疾皇子刁品,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容