php 检测负数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6135275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:26:28  来源:igfitidea点击:

Detecting negative numbers

phpcomparison-operators

提问by BigJobbies

I was wondering if there is any way to detect if a number is negative in PHP?

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

I have the following code:

我有以下代码:

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

I need to find out if $profitlossis negative and if it is, I need to echo out that it is.

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

回答by Mahn

I believe this is what you were looking for:

我相信这就是你要找的:

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) == "-");
    }
}

Usage:

用法:

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

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

Do however note that eval is a dangerous function, therefore use it only if you really, really need to find out if a number is negative.

但是请注意 eval 是一个危险的函数,因此仅当您确实需要确定数字是否为负数时才使用它。

:-)

:-)

回答by Dormouse

if ($profitloss < 0)
{
   echo "The profitloss is negative";
}

Edit: I feel like this was too simple an answer for the rep so here's something that you may also find helpful.

编辑:我觉得这对代表来说太简单了,所以这里有一些你可能会觉得有用的东西。

In PHP we can find the absolute value of an integer by using the abs()function. For example if I were trying to work out the difference between two figures I could do this:

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

$turnover = 10000;
$overheads = 12500;

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

echo "The Difference is ".$difference;

This would produce The Difference is 2500.

这将产生The Difference is 2500.

回答by Marty

if(x < 0)
if(abs(x) != x)
if(substr(strval(x), 0, 1) == "-")

回答by andrewmitchell

You could check if $profitloss < 0

你可以检查一下 $profitloss < 0

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

回答by Tudor Constantin

if ( $profitloss < 0 ) {
   echo "negative";
};

回答by Robson Vieira

Just multiply the number by -1 and check if the result is positive.

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

回答by Muhammad Sanaullah

Don't get me wrong, but you can do this way ;)

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

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>';
}
    }
}

Output:

输出:

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!

回答by AkaiNoKen

You could use a ternary operator like this one, to make it a one liner.

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

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

回答by user3402600

I assume that the main idea is to find if number is negative and display it in correct format.

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

For those who use PHP5.3 might be interested in using Number Formatter Class - http://php.net/manual/en/class.numberformatter.php. This function, as well as range of other useful things, can format your number.

对于那些使用 PHP5.3 的人可能有兴趣使用 Number Formatter Class - 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)

Here also a reference to why brackets are used for negative numbers: http://www.open.edu/openlearn/money-management/introduction-bookkeeping-and-accounting/content-section-1.7

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