用PHP表示费率计算的百分比金额
我正在使用一些涉及浮动和百分比利率的金融公式,在PHP代码中表示这些值时遇到了一些问题。我应该使用BC数学吗?我应该把我所有的百分比除以100吗?您将如何在PHP中表示以下公式 e、 g:金额的税额为8%,利率为每天1%。假设我想借X笔钱,15天内付清,分为3期,每期多少钱,总回报用PHP表示费率计算的百分比金额,php,Php,我正在使用一些涉及浮动和百分比利率的金融公式,在PHP代码中表示这些值时遇到了一些问题。我应该使用BC数学吗?我应该把我所有的百分比除以100吗?您将如何在PHP中表示以下公式 e、 g:金额的税额为8%,利率为每天1%。假设我想借X笔钱,15天内付清,分为3期,每期多少钱,总回报 totalTax = amount * 0.08 totalAmount = (amount + totalTax) interest = totalAmount * 0.01 * 15 perInstallment
totalTax = amount * 0.08
totalAmount = (amount + totalTax)
interest = totalAmount * 0.01 * 15
perInstallment = totalAmount + totalInterest / 3
关键的PHP函数是。我还在自定义函数中将类型转换为(float)。和往常一样,测试这段代码。我很好奇,如果你发现任何边缘情况下,这个数学与你的财务计算不同步。它通过了我的测试
function formatCurrency($input){
$result = number_format((float)$input, 2, '.', ',');
return $result;
}
$amount = 6458.56;
$totalTax = $amount * 0.08;
$totalAmount = $amount + $totalTax;
$interest = $totalAmount * 0.01 * 15;
$perInstallment = ($totalAmount + $interest) / 3;
echo 'Principal = $'.formatCurrency($amount).'<br/>';
echo 'Total Tax = $'.formatCurrency($totalTax).'<br/>';
echo 'Total Amount = $'.formatCurrency($totalAmount).'<br/>';
echo 'Total Interest = $'.formatCurrency($interest).'<br/>';
echo 'Each Installment = $'.formatCurrency($perInstallment).'<br/>';
函数格式货币($input){
$result=number_格式((float)$input,2',',',');
返回$result;
}
$amount=6458.56;
$totalTax=$amount*0.08;
$totalAmount=$amount+$totalTax;
$利息=$总金额*0.01*15;
$perInstallation=($totalAmount+$interest)/3;
echo“Principal=$”.formatCurrency($amount)。“
”;
echo“Total Tax=$”.formatCurrency($totalTax)。“
”;
回显'Total Amount=$'。格式货币($totalAmount)。'
;
echo“总利息=$”。格式货币($利息)。“
”;
echo“每期分期=$”.formatCurrency($perInstallation)。“
”;
关注您的财务运营:15天,每天1%
不是15%,而是16.1%。最好使用pow()函数而不是乘法运算符
在PHP中(例如,在命令行中运行):
根据@larsAnders的说法,它现在需要货币兑换。你发布的代码是否不起作用?使用此代码是否会收到任何错误消息?此处是否可以使用money_format函数格式化货币?money_格式有点粗略,因为它在Windows服务器上不起作用。从文档中可以看出:只有当系统具有strfmon功能时,才会定义money_format()函数。例如,Windows没有,所以money_format()在Windows中是未定义的。感谢您的澄清。如果我使用美分而不是浮动怎么办?公式有什么变化?如果你愿意,你可以用美分作为初始金额。但是,如果您需要避免浮动,那么pow()函数将没有任何用处。您必须递归地计算每日增长,计算顺序为:
$amount\u明天=$amount\u今天*101/100代码>或,相同:$a=$a+$a*1/100代码>。这够有用吗?
<?
$amount = 1000.0 ;
$tax = 1.08 ; // 8%
$interestPerDay = 1.01 ; // 1%/day
$days = 15 ;
$totalAmount = ($amount * $tax);
$totalAmountWithInterest = $totalAmount * pow($interestPerDay, $days) ;
$perInstallment = $totalAmountWithInterest / 3;
printf("Initial amout: %.2f\n", $amount);
printf("Amount tax inc.: %.2f\n", $totalAmount);
printf("Total amount: %.2f\n", $totalAmountWithInterest);
printf("Total interest: %.2f\n", $totalAmountWithInterest - $amount);
printf("Per installment: %.2f\n", $perInstallment );
Initial amout: 1000.00
Amount tax inc.: 1080.00
Total amount: 1253.85
Total interest: 253.85
Per installment: 417.95