找出 (n) PHP 最接近的较大倍数

发布于 2024-11-16 13:50:50 字数 295 浏览 2 评论 0原文

我有一个要求,用户被迫选择产品数量的 (n) 倍数。

(n) 值是根据每个产品设置的,可以是任意数字。

客户只能购买与产品设定的(n)数量倍数的产品数量。

假设 (n) 为 5,并且用户输入数量为 4 并说“添加到购物车”。我必须自动将该产品的数量添加为 5

如果用户输入 6 作为数量,那么我必须添加该产品的 10 数量。

我该怎么做呢?

我不明白这里应该应用什么逻辑。

I have a requirement where users are forced to choose the multiple of (n) quantity of a product.

The (n) value is set with each product that can be any number.

customer can only purchase the quantity of product in the multiple of (n) quantity set with product.

Suppose if (n) is 5 and user entered quantity as 4 and says Add to Cart. I have to add quantity of that product as 5 automatically.

and if user entered 6 as quantity then I have to add the 10 quantity of that product.

How I go about that?

I am not getting what logic should be applied here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

落在眉间の轻吻 2024-11-23 13:50:50
$entered_quantity = 6;
$suppose_n = 5;

$quantity = ceil($entered_quantity / $suppose_n) * $suppose_n;

echo $quantity;

打印 10

$entered_quantity = 6;
$suppose_n = 5;

$quantity = ceil($entered_quantity / $suppose_n) * $suppose_n;

echo $quantity;

prints 10

百善笑为先 2024-11-23 13:50:50

这不是 php 特定的;
你要做的就是计算。

上限(q / n) * n

其中 q 是用户的数量,
n 是重数

that's not php specific;
what you wonna do is to compute.

ceiling(q / n) * n

where q is the user's quantity,
n is the multiplicity

溺深海 2024-11-23 13:50:50

您可以尝试在除以给定的 n 时获取数字的余数,

例如:

$n = 5;
$amount = 6; // This would be the input, so replace the 6 with a $_POST/$_GET/etc.
$batches = floor($amount / $n);
$rest = $amount % $n;
if ($rest > 0) {
    $batches += 1;
    // You could give the user feedback here that they didn't put in a full multiple of $n
}
// $batches now contains the right amount of batches, so to get the total:
$total = $batches * $n;

当然,这可以压缩很多,但这可能会更好地概述所发生的情况:)。

You could try getting the remainder of the number when dividing by the given n

e.g.:

$n = 5;
$amount = 6; // This would be the input, so replace the 6 with a $_POST/$_GET/etc.
$batches = floor($amount / $n);
$rest = $amount % $n;
if ($rest > 0) {
    $batches += 1;
    // You could give the user feedback here that they didn't put in a full multiple of $n
}
// $batches now contains the right amount of batches, so to get the total:
$total = $batches * $n;

Ofcourse this can be condensed a lot, but this might give a better overview of what happens :).

秋千易 2024-11-23 13:50:50

尝试下面的功能。

function getNextMultipleOfFive($n) {
    $tmp=explode('.',($n/5));
    if($tmp[1]) {
        return ($tmp[0]+1)*5;
    }
    return $tmp[0]*5;
}

Try the below function.

function getNextMultipleOfFive($n) {
    $tmp=explode('.',($n/5));
    if($tmp[1]) {
        return ($tmp[0]+1)*5;
    }
    return $tmp[0]*5;
}
痴情换悲伤 2024-11-23 13:50:50

使用 do...while 循环:

$q = 6; // quantity by user input
$n = 5; // per purchace amount

$i = 0;
if ($q > 0)
{
    do
    {
        $i += $n;
    }
    while ($i <= $q);
}
echo $i; // 10

注意: 如果 $q >> 则不是很有效$n

With a do...while loop:

$q = 6; // quantity by user input
$n = 5; // per purchace amount

$i = 0;
if ($q > 0)
{
    do
    {
        $i += $n;
    }
    while ($i <= $q);
}
echo $i; // 10

Note: not very effective if $q >> $n

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文