63

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

我有以下代码:

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

我需要找出是否$profitloss是负面的,如果是,我需要回应它是。

4

11 回答 11

185
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.

于 2011-05-26T07:45:34.097 回答
180

我相信这就是你要找的:

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 是一个危险的函数,因此只有在您真的非常需要确定一个数字是否为负时才使用它。

:-)

于 2012-08-11T00:59:13.030 回答
20
if(x < 0)
if(abs(x) != x)
if(substr(strval(x), 0, 1) == "-")
于 2011-05-26T07:52:22.970 回答
7

你可以检查一下$profitloss < 0

if ($profitloss < 0):
    echo "Less than 0\n";
endif;
于 2011-05-26T07:46:37.200 回答
6
if ( $profitloss < 0 ) {
   echo "negative";
};
于 2011-05-26T07:46:07.917 回答
4

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

于 2015-10-22T11:33:35.880 回答
4

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

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!
于 2017-09-30T23:38:49.480 回答
2

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

echo ($profitloss < 0) ? 'false' : 'true';
于 2015-01-23T10:47:09.530 回答
0

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

对于那些使用 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

于 2015-07-01T13:46:00.537 回答
0

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

$is_negative = $profitloss < 0 ? true : false;
于 2020-11-13T06:45:48.670 回答
0

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

function isNegative($value){
    if(isset($value)) {
        if ((int)$value > 0) {
            return false;
        }
        return (int)$value < 0 && substr(strval($value), 0, 1) === "-";
    }
}
于 2021-07-02T06:57:02.587 回答