各种利息的计算方法

<?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, '.', ',' );
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,591评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,448评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,823评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,204评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,228评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,190评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,078评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,923评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,334评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,550评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,727评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,428评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,022评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,672评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,826评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,734评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,619评论 2 354

推荐阅读更多精彩内容