不正确的舍入问题,舍入而不是舍入

我有一个积分系统,目前,客户获得了分配的积分,并且每获得500积分将获得5英镑的代金券。

目前这就是我所拥有的,如果某人的得分为900,那么输出显示他们到目前为止已经赚了10英镑,这是不正确的。我该如何舍入,使其仅显示5英镑,然后当他们获得1000分以上时,它将显示10英镑,等等。

  <?php if($total >= 500) {
    $voucher_count = $total / 500;
    $voucher_rounded = round($voucher_count,0) . "<br />";
    $voucher_total = $voucher_rounded * 5; ?>
    <p class="earnings-to-date">You've earned £<?php echo $voucher_total; ?> so far</p>
  <?php } ?>
xxiaoabc 回答:不正确的舍入问题,舍入而不是舍入

地板-向下舍入小数
https://www.php.net/manual/en/function.floor.php


$total = 900;

if($total >= 500) {
    $voucher_count = $total / 500;
    $voucher_rounded = floor($voucher_count);
    $voucher_total = $voucher_rounded * 5; 
    echo $voucher_total; // Output: 5
}

,

仅需使用模运算符(%)在除以500之前就滤除多余的400(或其他内容):

$total = 900;

if($total >= 500) {
    $voucher_count = ($total - $total % 500) / 500;
    $voucher_total = $voucher_count * 5;
    echo $voucher_total;
}

输出:

5

取模运算符将计算除以指定数字的余数。在这种情况下:

($total - $total % 500) / 500;

计算出余数($total % 500 = 400),从$total中减去它,然后除以500

,

只需使用floor

$voucher_total = round(floor($total/500)) * 5;
本文链接:https://www.f2er.com/2993884.html

大家都在问