82

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.

1
  • Note that the reverse does not sound like the inverse of the first. If you change all the signs of your first set of numbers to negative, and then flip them all back to positive, you won't have the figures you started with.
    – Progrock
    Commented Jun 1, 2018 at 13:58

9 Answers 9

217

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 :)

10
  • Ops, yes sorry, just reversed the order:)
    – drAlberT
    Commented Sep 17, 2009 at 11:08
  • 2
    Ha, ok... for a second there I was doubting my own sanity.
    – Dan Tao
    Commented Sep 17, 2009 at 11:10
  • Why 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?
    – Gumbo
    Commented Sep 17, 2009 at 11:26
  • 1
    I could be wrong, but I'm pretty sure that since you're performing an operation under certain conditions ($num is positive) and not others ($num <= 0), you're always going to be using an if statement somewhere. Does abs($num) not simply perform $num = ($num >= 0) ? $num : -$num; under the hood?
    – Dan Tao
    Commented Sep 17, 2009 at 11:36
  • 1
    You are right, but it would be performed at low level, in C. By PHP, not in PHP.
    – drAlberT
    Commented Sep 17, 2009 at 11:46
56
$float = -abs($float);
3
  • 2
    "Also I need a way to do the reverse", this answer only does one thing...
    – ReSpawN
    Commented Jul 9, 2018 at 12:07
  • @ReSpawN To be fair, the reverse is literally just taking the - away. i.e. $float = abs($float);
    – Frits
    Commented May 23, 2019 at 10:19
  • value always will be negative. expected to mention it.
    – ShivarajRH
    Commented Sep 3, 2019 at 7:51
40

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;
    
0
18

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.

0
3

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); }
1
  • 3
    why to wrap abs() when used as is? nah.
    – drAlberT
    Commented Sep 17, 2009 at 13:45
3
function positive_number($number)
{
    if ($number < 0) {
        $number *= -1;
    }

   return $number;
}
0
function invertSign($value)
{
    return -$value;
}
2
  • Thanks for commenting why you downvoted. Really useful. Commented Dec 10, 2016 at 4:13
  • 1
    I 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".
    – Progrock
    Commented Jun 1, 2018 at 14:01
-1

using alberT and Dan Tao solution:

negative to positive and viceversa

$num = $num <= 0 ? abs($num) : -$num ;
-1

Here's a simple reusable code to inverse the sign of an int, float, double or decimal. If the value is positive, the method will return a negative value and vice versa.

function inverseSign($value)
{
    return $value * (-1);
}
2
  • Just curious, did you notice other answers? Commented Jan 5, 2023 at 12:12
  • Things can be done in different ways. Not only on way. Commented Jan 6, 2023 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.