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); }
function positive_number($number)
{
if ($number < 0) {
$number *= -1;
}
return $number;
}
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.
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;
$float = -abs($float);
using alberT and Dan Tao solution:
negative to positive and viceversa
$num = $num <= 0 ? abs($num) : -$num ;
function invertSign($value)
{
return -$value;
}
Source: Stackoverflow.com