Changing the sign of a number in PHP?
Solution 1
A trivial
$num = $num <= 0 ? $num : -$num ;
or, the better solution, IMHO:
$num = -1 * abs($num)
As @VegardLarsen has posted,
the explicit multiplication can be avoided for shortness but I prefer readability over shortness
I suggest to avoid if/else (or equivalent ternary operator) especially if you have to manipulate a number of items (in a loop or using a lambda function), as it will affect performance.
"If the float is a negative, make it a positive."
In order to change the sign of a number you can simply do:
$num = 0 - $num;
or, multiply it by -1, of course :)
Solution 2
$float = -abs($float);
Solution 3
How about something trivial like:
inverting:
$num = -$num;
converting only positive into negative:
if ($num > 0) $num = -$num;
converting only negative into positive:
if ($num < 0) $num = -$num;
Solution 4
re the edit: "Also i need a way to do the reverse If the float is a negative, make it a positive"
$number = -$number;
changes the number to its opposite.
Solution 5
I think Gumbo's answer is just fine. Some people prefer this fancy expression that does the same thing:
$int = (($int > 0) ? -$int : $int);
EDIT: Apparently you are looking for a function that will make negatives positive as well. I think these answers are the simplest:
/* I am not proposing you actually use functions called
"makeNegative" and "makePositive"; I am just presenting
the most direct solution in the form of two clearly named
functions. */
function makeNegative($num) { return -abs($num); }
function makePositive($num) { return abs($num); }
Comments
-
dotty almost 2 years
I have a few floats:
-4.50 +6.25 -8.00 -1.75
How can I change all these to negative floats so they become:
-4.50 -6.25 -8.00 -1.75
Also I need a way to do the reverse
If the float is a negative, make it a positive.
-
drAlberT over 14 yearsOps, yes sorry, just reversed the order:)
-
Dan Tao over 14 yearsHa, ok... for a second there I was doubting my own sanity.
-
Gumbo over 14 yearsWhy should getting the absolute value of a variable, inverting it and reassigning it to that variable no matter what value it is be more performant?
-
drAlberT over 14 yearsmaybe it is not, you are right. It would depend on the number of already OK numbers :)
-
Gumbo over 14 years-1
-1 * (0 - $num)
is equal to(-1 * 0) - (-1 * $num)
is equal to1 * $num
. -
Dan Tao over 14 yearsYour last answer is not correct! If $num is positive then -1 * (0 - $num) will return a positive number.
-
drAlberT over 14 yearsI just realized it, my bad sorry, removed ! Thank you
-
drAlberT over 14 yearsYou are right, but it would be performed at low level, in C. By PHP, not in PHP.
-
drAlberT over 14 yearswhy to wrap abs() when used as is? nah.
-
Frederik Krautwald over 7 yearsThanks for commenting why you downvoted. Really useful.
-
Progrock almost 6 yearsI think this is because the OP originally stated that they only want positive numbers to flip sign. But the question is rather confusing with the ambiguous bolt on: "Also I need a way to do the reverse".
-
ReSpawN almost 6 yearsIn my case the $num = 0 - $num was exactly what I needed. The one 'account' looses 100, the other 'gains' 100, which needs to be 'toggled', as it were.
-
ReSpawN almost 6 years"Also I need a way to do the reverse", this answer only does one thing...
-
Frits almost 5 years@ReSpawN To be fair, the reverse is literally just taking the
-
away. i.e.$float = abs($float);
-
ShivarajRH over 4 yearsvalue always will be negative. expected to mention it.
-
B001ᛦ almost 3 yearsPlease add a proper explanation to your answer. Also mention that your solutio needs to install an extra extension.
-
Eskay Amadeus almost 2 yearsInstalling an entire extension just to use a method isn't really worth it. But if the developer has other needs that the extension can satisfy, then it's a good option.