81

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
dotty
  • 40,405
  • 66
  • 150
  • 195
  • 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 Jun 01 '18 at 13:58

9 Answers9

215

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

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
drAlberT
  • 22,059
  • 5
  • 34
  • 40
  • Ops, yes sorry, just reversed the order:) – drAlberT Sep 17 '09 at 11:08
  • 2
    Ha, ok... for a second there I was doubting my own sanity. – Dan Tao Sep 17 '09 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 Sep 17 '09 at 11:26
  • maybe it is not, you are right. It would depend on the number of already OK numbers :) – drAlberT Sep 17 '09 at 11:28
  • -1 `-1 * (0 - $num)` is equal to `(-1 * 0) - (-1 * $num)` is equal to `1 * $num`. – Gumbo Sep 17 '09 at 11:30
  • Your last answer is not correct! If $num is positive then -1 * (0 - $num) will return a positive number. – Dan Tao Sep 17 '09 at 11:31
  • I just realized it, my bad sorry, removed ! Thank you – drAlberT Sep 17 '09 at 11:33
  • 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 Sep 17 '09 at 11:36
  • 1
    You are right, but it would be performed at low level, in C. _By PHP, not in PHP_. – drAlberT Sep 17 '09 at 11:46
  • 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 Jul 09 '18 at 09:57
57
$float = -abs($float);
Vegard Larsen
  • 12,827
  • 14
  • 59
  • 102
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;
    
Gumbo
  • 643,351
  • 109
  • 780
  • 844
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.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
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); }
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
3
function positive_number($number)
{
    if ($number < 0) {
        $number *= -1;
    }

   return $number;
}
Wallace Vizerra
  • 3,382
  • 2
  • 28
  • 29
0
function invertSign($value)
{
    return -$value;
}
Frederik Krautwald
  • 1,782
  • 23
  • 32
  • Thanks for commenting why you downvoted. Really useful. – Frederik Krautwald Dec 10 '16 at 04: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 Jun 01 '18 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);
}
Félix Maroy
  • 1,389
  • 2
  • 16
  • 21