Imagine a scenario where monthly rent is 100,000 which is payable at the end of each month. A tenant then decides to make a payment 350,000 to cater for the current month and those ahead. How do I distribute this amount since I can obviously see here that this amount caters for 3 months and a half month? Here is what I was trying in PHP but I just can't get the last 50,000 to appear.
$rent = 100000; // rent amount
$amountPaid = 350000; // amount paid by tenant
$length = $amountPaid/$rent; // number of months paid for
for ($c = 1; $c <= $length; $c++)
{
$foreachMonth = $rent;
assignRentFunction($c, $foreachMonth);
}
function assignRentFunction($count, $amt)
{
echo "Month ".$count.': '.$amt."<br>";
}
答案 0 :(得分:6)
步骤:
1)使用ceil()
函数获得总计月份。
2)将返回4个月。 3个月全额支付,一个月仅支付50000。
3)现在,每个循环将使总租金增加10000。
4)如果超过付款金额,则获得mod为50000
$rent = 100000; // rent amount
$amountPaid = 350000; // amount paid by tenant
$length = ceil($amountPaid/$rent); // number of months paid for
$totalRent = 0;
for ($c = 1; $c <= $length; $c++) {
$totalRent += $rent;
$foreachMonth = $rent;
if ($amountPaid < $totalRent) { // Here is the logic, if amount exceeds, use the remaining amount.
$foreachMonth = $amountPaid % $rent;
}
assignRentFunction($c, $foreachMonth);
}
function assignRentFunction($count, $amt) {
echo "Month ".$count.': '.$amt."<br>";
}
**Output:**
Month 1: 100000
Month 2: 100000
Month 3: 100000
Month 4: 50000
答案 1 :(得分:5)
将循环部分更改为
for ($c = 1; $c <= $length; $c++) {
$foreachMonth = $rent;
assignRentFunction($c, $foreachMonth);
}
$fractionalMonth = $length - intval($length);
if ($fractionalMonth)
assignRentFunction($c, $foreachMonth * $fractionalMonth);
您的错误是因为您总是将$c
加1,所以无法获得上个月的小数部分
答案 2 :(得分:5)
您可以像这样解决您的问题,
View vv((TestQuestion)getActivity()).findViewById(R.id.count_down_strip);
bbb=vv.findViewById(R.id.finish);
Demo。
答案 3 :(得分:4)
由于似乎有很多方法可以切片,所以我想我也应该把帽子戴在戒指上
function makeColor(colorNum, colors){
if (colors < 1) colors = 1;
// defaults to one color - avoid divide by zero
return colorNum * (360 / colors) % 360;
}
// This could be length of your array.
var totalDIVs = 20;
var totalColors = totalDIVs;
for (var i = 0; i < totalDIVs; i++){
var element = document.createElement('div');
document.body.appendChild(element);
var color = "hsl( " + makeColor(i, totalColors) + ", 100%, 50% )";
element.style.backgroundColor = color;
element.innerHTML = color;
}
现在是评论版本:
for($c = 1; $c<=ceil($amountPaid/$rent); $c++){
assignRentFunction($c, $rent - max(($c * $rent - $amountPaid),0));
}
答案 4 :(得分:4)
$rent= 100000; // rent amount
$amountPaid= 350000; // amount paid by tenant
$length= $amountPaid/$rent; // number of months paid for
for ($c = 1; $c <= ceil($length); $c++)
{
$foreachMonth = 100000;
if($amountPaid>$rent)
{
$rent=$rent;
$amountPaid=$amountPaid-$rent;
}
else
{
$rent=$rent-$amountPaid;
}
assignRentFunction($c, $rent);
}
function assignRentFunction($count, $amt)
{
echo "Month ".$count.': '.$amt."<br>";
}