【问题标题】:Detecting negative numbers检测负数
【发布时间】:2011-09-02 09:05:10
【问题描述】:

我想知道是否有任何方法可以检测 PHP 中的数字是否为负数?

我有以下代码:

$profitloss = $result->date_sold_price - $result->date_bought_price;

我需要确定$profitloss 是否为负数,如果是,我需要回应它。

【问题讨论】:

  • 别担心,我们都有糟糕的日子;)
  • Ohh dang .... 这么简单的解决方案.. grr
  • 这只是运气不好^^请看我下面的回复,只是为了好玩伙伴:D
  • 这太棒了,因为我确实在这个页面上,因为我有同样的问题,但幸运的是我在看到答案之前就记住了。它是如此简单,但它只是表明我们很多时候过于复杂了。

标签: php comparison-operators


【解决方案1】:
if ($profitloss < 0)
{
   echo "The profitloss is negative";
}

编辑:我觉得这对代表来说太简单了,所以这里的内容可能对您也有帮助。

在 PHP 中,我们可以使用 abs() 函数找到整数的绝对值。例如,如果我想计算两个数字之间的差异,我可以这样做:

$turnover = 10000;
$overheads = 12500;

$difference = abs($turnover-$overheads);

echo "The Difference is ".$difference;

这将产生The Difference is 2500

【讨论】:

  • else { echo "万岁..."; }
  • 我不明白你的意思,这个需要一些认真的锻炼。
  • 很好的答案,我也喜欢你的编辑。但是与$difference = $turnover - $overheads; 有什么区别?
  • 哦,哇,没想到将近三年后才发表评论。 abs() 将返回任何整数的绝对值,因此 abs(-100) 返回 100。$turnover-$overheads 将导致 -2500 而abs($turnover-$overheads) 将导致 2500。
【解决方案2】:

我相信这就是你要找的东西:

class Expression {
    protected $expression;
    protected $result;

    public function __construct($expression) {
        $this->expression = $expression;
    }

    public function evaluate() {
        $this->result = eval("return ".$this->expression.";");
        return $this;
    }

    public function getResult() {
        return $this->result;
    }
}

class NegativeFinder {
    protected $expressionObj;

    public function __construct(Expression $expressionObj) {
        $this->expressionObj = $expressionObj;
    }

    public function isItNegative() {
        $result = $this->expressionObj->evaluate()->getResult();

        if($this->hasMinusSign($result)) {
            return true;
        } else {
            return false;
        }
    }

    protected function hasMinusSign($value) {
        return (substr(strval($value), 0, 1) == "-");
    }
}

用法:

$soldPrice = 1;
$boughtPrice = 2;
$negativeFinderObj = new NegativeFinder(new Expression("$soldPrice - $boughtPrice"));

echo ($negativeFinderObj->isItNegative()) ? "It is negative!" : "It is not negative :(";

但是请注意 eval 是一个危险的函数,因此只有在你真的非常需要确定一个数字是否为负时才使用它。

:-)

【讨论】:

  • 你先生,是一个可怕的,可怕的人:))
  • 天哪,这太棒了。
  • 虽然我没有对此进行测试,但它看起来会检测到-0
  • 这让我很开心!谢谢。
  • 从现在开始,我可以用这个答案作为我对“过度工程”的定义。
【解决方案3】:
if(x < 0)
if(abs(x) != x)
if(substr(strval(x), 0, 1) == "-")

【讨论】:

    【解决方案4】:

    你可以检查$profitloss &lt; 0

    if ($profitloss < 0):
        echo "Less than 0\n";
    endif;
    

    【讨论】:

      【解决方案5】:
      if ( $profitloss < 0 ) {
         echo "negative";
      };
      

      【讨论】:

        【解决方案6】:

        只需将数字乘以 -1 并检查结果是否为正。

        【讨论】:

        • 将代码添加到答案中,我会给你 +1。 :)
        【解决方案7】:

        不要误会我的意思,但你可以这样做;)

        function nagitive_check($value){
        if (isset($value)){
            if (substr(strval($value), 0, 1) == "-"){
            return 'It is negative<br>';
        } else {
            return 'It is not negative!<br>';
        }
            }
        }
        

        输出:

        echo nagitive_check(-100);  // It is negative
        echo nagitive_check(200);  // It is not negative!
        echo nagitive_check(200-300);  // It is negative
        echo nagitive_check(200-300+1000);  // It is not negative!
        

        【讨论】:

          【解决方案8】:

          您可以使用像这样的三元运算符,使其成为单线。

          echo ($profitloss < 0) ? 'false' : 'true';
          

          【讨论】:

          • @Shelton 我根据你的建议修正了答案,并添加了一个简短的解释。
          【解决方案9】:

          我假设主要思想是查找数字是否为负数并以正确的格式显示。

          对于那些使用 PHP5.3 的人可能有兴趣使用数字格式化类 - http://php.net/manual/en/class.numberformatter.php。这个功能,以及其他一些有用的东西,可以格式化你的号码。

          $profitLoss = 25000 - 55000;
          
          $a= new \NumberFormatter("en-UK", \NumberFormatter::CURRENCY); 
          $a->formatCurrency($profitLoss, 'EUR');
          // would display (€30,000.00)
          

          这里还参考了为什么括号用于负数: http://www.open.edu/openlearn/money-management/introduction-bookkeeping-and-accounting/content-section-1.7

          【讨论】:

            【解决方案10】:

            可以用三元运算符轻松实现。

            $is_negative = $profitloss < 0 ? true : false;
            

            【讨论】:

            • 表达式已经是一个布尔值,所以不需要三进制。它与 $is_negative = $profitloss 完全相同
            【解决方案11】:

            我为我的 Laravel 项目编写了一个 Helper 函数,但可以在任何地方使用。

            function isNegative($value){
                if(isset($value)) {
                    if ((int)$value > 0) {
                        return false;
                    }
                    return (int)$value < 0 && substr(strval($value), 0, 1) === "-";
                }
            }
            

            【讨论】:

              猜你喜欢
              • 2021-05-21
              • 1970-01-01
              • 2011-06-09
              • 2021-01-24
              • 2017-02-04
              • 2012-11-25
              • 2012-05-12
              • 2013-03-20
              相关资源
              最近更新 更多