检测负数

发布于 2024-11-09 23:09:38 字数 252 浏览 4 评论 0原文

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

我有以下代码:

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

我需要找出 $profitloss 是否为负数,如果是,我需要回显它是负数。

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

I have the following code:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

慕巷 2024-11-16 23:09:38
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

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:

$turnover = 10000;
$overheads = 12500;

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

echo "The Difference is ".$difference;

This would produce The Difference is 2500.

德意的啸 2024-11-16 23:09:38

我相信这就是您正在寻找的:

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

:-)

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.

:-)

明天过后 2024-11-16 23:09:38
if(x < 0)
if(abs(x) != x)
if(substr(strval(x), 0, 1) == "-")
if(x < 0)
if(abs(x) != x)
if(substr(strval(x), 0, 1) == "-")
绳情 2024-11-16 23:09:38

您可以检查是否 $profitloss < 0

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

You could check if $profitloss < 0

if ($profitloss < 0):
    echo "Less than 0\n";
endif;
一桥轻雨一伞开 2024-11-16 23:09:38
if ( $profitloss < 0 ) {
   echo "negative";
};
if ( $profitloss < 0 ) {
   echo "negative";
};
只是我以为 2024-11-16 23:09:38

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

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!

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!
春风十里 2024-11-16 23:09:38

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

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

大姐,你呐 2024-11-16 23:09:38

您可以使用像这样的三元运算符,使其成为一个衬垫。

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

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

echo ($profitloss < 0) ? 'false' : 'true';
怎樣才叫好 2024-11-16 23:09:38

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

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

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.

$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

旧情勿念 2024-11-16 23:09:38

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

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

Can be easily achieved with a ternary operator.

$is_negative = $profitloss < 0 ? true : false;
浅唱ヾ落雨殇 2024-11-16 23:09:38

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

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

I wrote a Helper function for my Laravel project but can be used anywhere.

function isNegative($value){
    if(isset($value)) {
        if ((int)$value > 0) {
            return false;
        }
        return (int)$value < 0 && substr(strval($value), 0, 1) === "-";
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文