Jump to content

from negative to positive


Baabu

Recommended Posts

Hrm, it seems I was overthinking things!!

 

Here you go

 

<?php
function neg2pos($number)
{
return ($number < 0) ? $number * -1 : $number;
}

echo neg2pos(22.22); // returns 22.22
echo neg2pos(-599.22); // returns -599.22

?>

 

I hope it does everything you need it to.

 

And of course you go offline when I finally figure it out :(

or just negate the variable with a minus sign

 

<?php

function neg2pos($number)
{
    return ($number < 0) ? -$number : $number;
}

 

 

Ooh nice catch!

 

I wonder if there is a way to get this function to be built in to PHP?  one for neg2pos and pos2neg could be useful.

Good call, I knew there had to be a built in function to handle it.

 

the question though is, is there a pos2neg function built in (the opposite)?

 

neg2pos or pos2neg

 

you just apply the minus sign to switch from one to the other.

 

$profit = 1000;

echo -$profit;    // -1000

 

abs() always returns positive.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.