Baabu Posted March 7, 2008 Share Posted March 7, 2008 is there any function in php which can convert any negative number to positive number just like somefunction(-20); and result should be 20 Any ideas?? ??? Quote Link to comment Share on other sites More sharing options...
Naez Posted March 7, 2008 Share Posted March 7, 2008 If you are always going to be dealing with a negative number, you can just multiply that by -1. But if its mixed input and you always want it to be positive, then I'm not sure! sorry! Quote Link to comment Share on other sites More sharing options...
Baabu Posted March 7, 2008 Author Share Posted March 7, 2008 the case is just like if u have profit then it is in positive else if it negative then it is loss and i want that loss to be stored in aa a positive number Quote Link to comment Share on other sites More sharing options...
Naez Posted March 7, 2008 Share Posted March 7, 2008 I am working on a function for you... that only works with int's atm.... I'm gonna try to get it to work on floats (in case your loss is like 410.22) So unless someone says some built in PHP function I should have one I wrote for you in a few mins Quote Link to comment Share on other sites More sharing options...
Naez Posted March 7, 2008 Share Posted March 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
Naez Posted March 7, 2008 Share Posted March 7, 2008 Bleh, in my comment I said it would return -599.22...but I meant it will return the positive value (599.22) >.< for some reason cant edit my post! Quote Link to comment Share on other sites More sharing options...
Barand Posted March 7, 2008 Share Posted March 7, 2008 or just negate the variable with a minus sign <?php function neg2pos($number) { return ($number < 0) ? -$number : $number; } Quote Link to comment Share on other sites More sharing options...
Baabu Posted March 7, 2008 Author Share Posted March 7, 2008 thx for ur help guyz both worked for me Quote Link to comment Share on other sites More sharing options...
Naez Posted March 7, 2008 Share Posted March 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted March 7, 2008 Share Posted March 7, 2008 or, simpler <?php $loss = -500; echo -$loss; // 500 ?> Quote Link to comment Share on other sites More sharing options...
KrisNz Posted March 7, 2008 Share Posted March 7, 2008 I'm not the most mathematically inclined person, but isn't that what abs() is for? Quote Link to comment Share on other sites More sharing options...
Naez Posted March 7, 2008 Share Posted March 7, 2008 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)? Or I guess you could just do $var = abs(20); $var = -$var Quote Link to comment Share on other sites More sharing options...
Baabu Posted March 7, 2008 Author Share Posted March 7, 2008 yeah abs() i just seen that Quote Link to comment Share on other sites More sharing options...
Barand Posted March 7, 2008 Share Posted March 7, 2008 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. Quote Link to comment Share on other sites More sharing options...
Naez Posted March 7, 2008 Share Posted March 7, 2008 echo -abs(500); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.