Loading

PHP 各种金融利息的计算方法

<?php
/**
 * 人民币小写转大写
 *
 * @param string $number   待处理数值
 * @param bool   $is_round 小数是否四舍五入,默认"四舍五入"
 * @param string $int_unit 币种单位,默认"元"
 * @return string
 */
function rmb_format($money = 0, $is_round = true, $int_unit = '元') {
    $chs     = array (0, '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');
    $uni     = array ('', '拾', '佰', '仟' );
    $dec_uni = array ('角', '分' );
    $exp     = array ('','万','亿');
    $res     = '';
    // 以 元为单位分割
    $parts   = explode ( '.', $money, 2 );
    $int     = isset ( $parts [0] ) ? strval ( $parts [0] ) : 0;
    $dec     = isset ( $parts [1] ) ? strval ( $parts [1] ) : '';
    // 处理小数点
    $dec_len = strlen ( $dec );
    if (isset ( $parts [1] ) && $dec_len > 2) {
        $dec = $is_round ? substr ( strrchr ( strval ( round ( floatval ( "0." . $dec ), 2 ) ), '.' ), 1 ) : substr ( $parts [1], 0, 2 );
    }
    // number= 0.00时,直接返回 0
    if (empty ( $int ) && empty ( $dec )) {
        return '零';
    }

    // 整数部分 从右向左
    for($i = strlen ( $int ) - 1, $t = 0; $i >= 0; $t++) {
        $str = '';
        // 每4字为一段进行转化
        for($j = 0; $j < 4 && $i >= 0; $j ++, $i --) {
            $u   = $int{$i} > 0 ? $uni [$j] : '';
            $str = $chs [$int {$i}] . $u . $str;
        }
        $str = rtrim ( $str, '0' );
        $str = preg_replace ( "/0+/", "零", $str );
        $u2  = $str != '' ? $exp [$t] : '';
        $res = $str . $u2 . $res;
    }
    $dec = rtrim ( $dec, '0' );
    // 小数部分 从左向右
    if (!empty ( $dec )) {
        $res .= $int_unit;
        $cnt =  strlen ( $dec );
        for($i = 0; $i < $cnt; $i ++) {
            $u = $dec {$i} > 0 ? $dec_uni [$i] : ''; // 非0的数字后面添加单位
            $res .= $chs [$dec {$i}] . $u;
        }
        if ($cnt == 1) $res .= '整';
        $res = rtrim ( $res, '0' ); // 去掉末尾的0
        $res = preg_replace ( "/0+/", "零", $res ); // 替换多个连续的0
    } else {
        $res .= $int_unit . '整';
    }
    return $res;
}

/**
 * 一次性本息的利息计算
 *
 * @param int $money 本金
 * @param float $annualInterestRate 年化收益率
 * @param int $day 天数
 * @return number 利息
 */
function interest_day($money, $annualInterestRate, $day)
{
    $interestRate = $annualInterestRate / 365;
    return $money * $interestRate * $day;
}

/**
 * 利息计算
 *
 * @param int $money
 * @param floal $annualInterestRate
 * @param int $month
 * @param int $type 0 一次性本息 1 等额本金 2等额本息 3每月还息到期还本
 */
function interest_count($money, $annualInterestRate, $month, $type = 0)
{
    // 月利率
    $interestRate = $annualInterestRate / 12;
    $res = array ();
    if ($type == 0) { // 一次性本息
        $interest = $money * $interestRate * $month;
        $res [] = array ('total' => $money + $interest,'money' => $money,'interest' => $interest,'nper' => 1 );
    } elseif ($type == 1) { // 等额本息
                            // 每月月供额=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1〕
        $trate = $interestRate + 1;
        // $P = round ( ($money * $interestRate * pow ( $trate, $month )) / (pow ( $trate, $month ) - 1), 2 );
        // 每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
        $res = array ();
        for($i = 1; $i <= $month; $i ++) {
            $a = $money * $interestRate * pow ( $trate, $i - 1 );
            $b = pow ( $trate, $month ) - 1;
            // 每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
            $c = $money * $interestRate * (pow ( $trate, $month ) - pow ( $trate, $i - 1 ));
            $res [$i] ['money'] = round ( $a / $b, 2 );
            $res [$i] ['interest'] = round ( $c / $b, 2 );
            $res [$i] ['nper'] = $i;
            $res [$i] ['total'] = round ( $res [$i] ['money'] + $res [$i] ['interest'], 2 );
        }
    } elseif ($type == 2) { // 等额本金
        $principal = $money / $month;
        $res = array ();
        for($i = 1; $i <= $month; $i ++) {
            // 每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率
            $interest = $money * $interestRate;
            $res [$i] = array ('total' => $principal + $interest,'money' => $principal,'interest' => $interest,'nper' => $i,'diminishing' => $principal * $interestRate );
            // 剩余本金
            $money = $money - $principal;
        }
    } elseif ($type == 3) {
        for($i = 1; $i <= $month; $i ++) {
            $interest = $money * $interestRate;
            if ($i == $month) {
                $res [$i] = array ('total' => $money + $interest,'money' => $money,'interest' => $interest,'nper' => $i );
            } else {
                $res [$i] = array ('total' => $interest,'money' => 0,'interest' => $interest,'nper' => $i );
            }
        }
    }
    return $res;
}

/**
 * 一次性本息
 *
 * @param unknown $a
 */
function aaa($cash, $rate, $month)
{
    // 月利率
    $rate = $rate / 12;
    $c = $cash * $rate * $month;
    $res = [ 'total' => $cash + $c,'cash' => $cash,'rate' => $c ];
    return $c;
}

/**
 * 等额本息
 *
 * @param int $cash 贷款总额
 * @param unknown $rate 年化收益率
 * @param int $month 贷款月数
 */
function bbb($cash, $rate, $month)
{
    $rate = $rate / 12;
    // 每月月供额=〔贷款本金×月利率×(1+月利率)^还款月数〕÷〔(1+月利率)^还款月数-1〕
    $trate = $rate + 1;
    $a = $cash * $rate * pow ( $trate, $month );
    $b = pow ( $trate, $month ) - 1;
    $P = round ( $a / $b, 2 );
    // 每月应还本金=贷款本金×月利率×(1+月利率)^(还款月序号-1)÷〔(1+月利率)^还款月数-1〕
    $res = array ();
    for($i = 1; $i <= $month; $i ++) {
        $a = $cash * $rate * pow ( $trate, $i - 1 );
        $b = pow ( $trate, $month ) - 1;
        // 每月应还利息=贷款本金×月利率×〔(1+月利率)^还款月数-(1+月利率)^(还款月序号-1)〕÷〔(1+月利率)^还款月数-1〕
        $c = $cash * $rate * (pow ( $trate, $month ) - pow ( $trate, $i - 1 ));
        $res [$i] ['cash'] = round ( $a / $b, 2 );
        $res [$i] ['rate'] = round ( $c / $b, 2 );
        $res [$i] ['total'] = round ( $res [$i] ['cash'] + $res [$i] ['rate'], 2 );
    }
    // 总利息=还款月数×每月月供额-贷款本金
    return $res;
}

/**
 * 等额本金
 *
 * @param int $cash 贷款本金
 * @param unknown $rate 年化收益率
 * @param int $month 贷款月数
 */
function ccc($cash, $rate, $month)
{
    $rate = $rate / 12;
    // 每月应还本金=贷款本金÷还款月数 2500
    $a = $cash / $month;
    // 每月应还利息=剩余本金×月利率
    // $b = $cash * $rate;
    // 每月月供递减额 = 每月应还本金×月利率 = 贷款本金÷还款月数×月利率
    // $c = $a * $rate;
    $res = array ();
    for($i = 1; $i <= $month; $i ++) {
        // 每月应还利息=剩余本金×月利率=(贷款本金-已归还本金累计额)×月利率
        $d = $cash * $rate;
        // 剩余本金
        $cash = $cash - $a;
        $res [$i] = [ 'total' => $a + $d,'cash' => $a,'rate' => $d ];
    }
    // print_r ( $res );
    return $res;
}

/**
 * 每月还息到期还本
 */
function ddd($cash, $rate, $month)
{
    // 月利率
    $rate = $rate / 12;
    $res = array ();
    for($i = 1; $i <= $month; $i ++) {
        $c = $cash * $rate;
        if ($i == $month) {
            $res [$i] = [ 'total' => $cash + $c,'cash' => $cash,'rate' => $c ];
        } else {
            $res [$i] = [ 'total' => $c,'cash' => 0,'rate' => $c ];
        }
    }
    return $res;
}
/**
 * 按年付息到期还本
 *
 * @param unknown $cash
 * @param unknown $rate
 * @param unknown $month
 * @return multitype:multitype:number unknown
 */
function eee($cash, $rate, $month)
{
    $res = array ();
    for($i = 1; $i <= $month; $i ++) {
        $c = $cash * $rate;
        if ($i == $month) {
            $res [$i] = [ 'total' => $cash + $c,'cash' => $cash,'rate' => $c ];
        } else {
            $res [$i] = [ 'total' => $c,'cash' => 0,'rate' => $c ];
        }
    }
    return $res;
}

/**
 * 格式化显示钱数
 *
 * @param unknown $amount
 * @return string
 */
function amount_format($amount)
{
    return number_format ( $amount, 2, '.', ',' );
}

posted @ 2020-04-01 11:52  方圆百里找对手  阅读(470)  评论(0编辑  收藏  举报