【问题标题】:Is there a specific function that allows me to round up to a specific decimal number in PHP是否有一个特定的函数可以让我在 PHP 中四舍五入到特定的十进制数
【发布时间】:2021-08-01 14:10:15
【问题描述】:

我有一个数字需要四舍五入到特定的小数,PHP 中有什么函数可以做到这一点吗?

我需要每个数字(反映金额)都有一个特定的十进制数字。 例如:

小数点必须是 25,所以如果我得到 25.50 美元,我需要它是 26 美元。25,如果我得到 25.10 美元,它需要是25 美元。25

我检查了 PHP round(),特别是 ceil(),我遇到了 this answer in Python,但我不确定它是否适用于我的情况,因为我需要的是不同的。

有什么想法吗?甚至伪代码作为从哪里开始的提示也会对我有所帮助。谢谢!

【问题讨论】:

  • 如果你有 25.49 美元,你想要 26.25 美元还是 25.25 美元? (只是想知道它是否可以四舍五入)
  • 不要将钱存为实数。大多数实数不能在计算机中精确表示。舍入没有帮助,因为它将一个不精确的数字转换为另一个很可能也不精确的数字。如果您只需要管理一种货币,最好的选择是以最小单位存储金额(美分代表美元,欧分代表欧元等),并且仅在显示时将其表示为美元+美分。
  • 我需要四舍五入,所以应该是 26.25。感谢您的评论@axiac,实际上是我没有想到的。所以你的意思是在一个单元格中存储美元 aumont(即 40)而在另一个单元格中存储美分(即 25)?当我需要显示我将两者放在一起时有多少?
  • @Rosamunda 使用一个变量来保存总分值(包括“美元”值作为100 的倍数)。不需要有两个变量。
  • 货币的基础是floating point precision会导致长期不准确(0.1 + 0.2 === 0.3 (false))。因此,您可以将货币值存储为整数,然后将该值除以最小分母$currency = $amount / 100 另一种解决方案是使用bc math functions

标签: php rounding


【解决方案1】:

我认为您需要一个自定义函数,如下所示:

function my_round($number, $decimal = 0.25) {
  $result = floor($number) + $decimal;
  if ($result < $number) $result = ceil($number) + $decimal;
  return $result;
}

print my_round(25.50);

【讨论】:

  • 好的,在新版本中修复,一直在盈利)
  • 为什么是round 而不是floor?这已破了。尝试使用$decimal=0.99 并输入25.5025.99 之间的任何内容。它会四舍五入到26.99
  • @mpen 是,固定为十进制 0.5+
【解决方案2】:

我为你的情况修改了this answer

<?php
function roundUp($number){
    $int = floor($number);
    $float = $number-$int;
    if ($float*10 < 2.5)
        $result = $int;
    else
        $result = ceil($number);
    $result+= 0.25;
    echo $number." becomes ".$result."\n";
}

roundUp(25.50);
roundUp(25.10);

寻找demo here

【讨论】:

    【解决方案3】:

    遵循 cmets 中提到的 axiac 的建议和 this thread,在货币环境中处理浮点数的最佳方法是将美元和美分的价值视为 2 个独立的实体。

    我能想到的一种方法是将小数点前后的数字分成 2 个单独的变量并进行相应的处理。

    <?php
    
    function customRound($amount){
       $amount = strval($amount);
       if(preg_match('/(\d+)\.?(\d{1,2})?/', $amount, $matches) !== 1){
          throw new \Exception("Invalid amount.");
       }
       $dollars = intval($matches[1]);
       $cents = intval($matches[2] ?? 0);
       if($cents < 10) $cents *= 10;
       if($cents <= 25) return $dollars . ".25";
       return ($dollars + 1) . ".25";
    }
    
    $tests = [25.51,25.49,26.25,25.10,25.49];
    
    foreach ($tests as $test){
        echo $test," => ",customRound($test),PHP_EOL;
    } 
    

    【讨论】:

      【解决方案4】:

      这是另一种方法:

      <?php
      
      function roundUp($number, $decimal=0.25){
          $dollars = floor($number);
          $cents = $number - $dollars;
          if($cents > $decimal) {
              ++$dollars;
          }
          return $dollars + $decimal;
      }
      
      echo roundUp(25.50).PHP_EOL;
      echo roundUp(25.10);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多