Changing the sign of a number in PHP?

137,854

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); }
Share:
137,854
dotty
Author by

dotty

Hmm not alot really.

Updated on July 05, 2022

Comments

  • dotty
    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
    drAlberT over 14 years
    Ops, yes sorry, just reversed the order:)
  • Dan Tao
    Dan Tao over 14 years
    Ha, ok... for a second there I was doubting my own sanity.
  • Gumbo
    Gumbo over 14 years
    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?
  • drAlberT
    drAlberT over 14 years
    maybe it is not, you are right. It would depend on the number of already OK numbers :)
  • Gumbo
    Gumbo over 14 years
    -1 -1 * (0 - $num) is equal to (-1 * 0) - (-1 * $num) is equal to 1 * $num.
  • Dan Tao
    Dan Tao over 14 years
    Your last answer is not correct! If $num is positive then -1 * (0 - $num) will return a positive number.
  • drAlberT
    drAlberT over 14 years
    I just realized it, my bad sorry, removed ! Thank you
  • drAlberT
    drAlberT over 14 years
    You are right, but it would be performed at low level, in C. By PHP, not in PHP.
  • drAlberT
    drAlberT over 14 years
    why to wrap abs() when used as is? nah.
  • Frederik Krautwald
    Frederik Krautwald over 7 years
    Thanks for commenting why you downvoted. Really useful.
  • Progrock
    Progrock almost 6 years
    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".
  • ReSpawN
    ReSpawN almost 6 years
    In 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
    ReSpawN almost 6 years
    "Also I need a way to do the reverse", this answer only does one thing...
  • Frits
    Frits almost 5 years
    @ReSpawN To be fair, the reverse is literally just taking the - away. i.e. $float = abs($float);
  • ShivarajRH
    ShivarajRH over 4 years
    value always will be negative. expected to mention it.
  • B001ᛦ
    B001ᛦ almost 3 years
    Please add a proper explanation to your answer. Also mention that your solutio needs to install an extra extension.
  • Eskay Amadeus
    Eskay Amadeus almost 2 years
    Installing 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.