我有一个客户的租用计算器示例,该计算器将金额每3个月除1次。这是示例:
金额是:320.49 付款设置为4付款
library(tidyverse)
# Example data
df <- tibble(col1 = c(1, 2, 3, NA),
col2 = c(NA, NA, "a", "b"))
df %>% summarise_all(~ sum(is.na(.)))
#> # A tibble: 1 x 2
#> col1 col2
#> <int> <int>
#> 1 1 2
默认情况下,我将320.49 / 4除以得到80.1225
通过四舍五入80.1225,我得到80.12 * 4 =320.48,低于320.49
如果金额不能与付款次数相除,我应该使用什么原则来使除最后一次付款外的所有付款均等?
谢谢您的时间!
答案 0 :(得分:1)
如果要使用PHP计算付款,则有两个选择。要使最后一次付款最大,请使用以下代码:
$amount = 320.49;
$payments = array_fill(0, 3, floor($amount * 100 / 4) / 100);
$payments[3] = round($amount - $payments[0] * 3, 2);
print_r($payments);
输出:
Array
(
[0] => 80.12
[1] => 80.12
[2] => 80.12
[3] => 80.13
)
要使最后一次付款最小,请使用以下代码:
$payments = array_fill(0, 3, ceil($amount * 100 / 4) / 100);
$payments[3] = round($amount - $payments[0] * 3, 2);
print_r($payments);
输出:
Array
(
[0] => 80.13
[1] => 80.13
[2] => 80.13
[3] => 80.1
)
答案 1 :(得分:0)
您可以使用它来获取剩余金额,并避免PHP Floating point precision
的问题:
$tot = 320.49;
$qty = 4;
echo $singlePayment = number_format(($tot / $qty), 2, '.', '')+0;
echo '<br>';
echo $exceed = number_format(($tot / $qty) - number_format(($tot / $qty), 2, '.', ''), 10, '.', '')*$qty+0;
if($exceed > 0)
{
// add this to the last payment
}
输出:
80.12
0.01