Detecting negative numbers

PhpComparison Operators

Php Problem Overview


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.

Php Solutions


Solution 1 - Php

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.

Solution 2 - Php

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.

:-)

Solution 3 - Php

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

Solution 4 - Php

You could check if $profitloss < 0

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

Solution 5 - Php

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

Solution 6 - Php

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!

Solution 7 - Php

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

Solution 8 - Php

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

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

Solution 9 - Php

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

Solution 10 - Php

Can be easily achieved with a ternary operator.

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

Solution 11 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBigJobbiesView Question on Stackoverflow
Solution 1 - PhpDormouseView Answer on Stackoverflow
Solution 2 - PhpMahnView Answer on Stackoverflow
Solution 3 - PhpMartyView Answer on Stackoverflow
Solution 4 - PhpandrewmitchellView Answer on Stackoverflow
Solution 5 - PhpTudor ConstantinView Answer on Stackoverflow
Solution 6 - PhpMuhammad SanaullahView Answer on Stackoverflow
Solution 7 - PhpRobson VieiraView Answer on Stackoverflow
Solution 8 - PhpAkaiNoKenView Answer on Stackoverflow
Solution 9 - PhpVlad Vladimir HerculesView Answer on Stackoverflow
Solution 10 - PhpUsman AhmedView Answer on Stackoverflow
Solution 11 - PhpNauman BashirView Answer on Stackoverflow