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?? ??? Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/ 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! Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485853 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 Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485855 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 Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485858 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 Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485863 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! Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485866 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; } Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485870 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 Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485876 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. Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485878 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 ?> Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485879 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? Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485880 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 Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485882 Share on other sites More sharing options...
Baabu Posted March 7, 2008 Author Share Posted March 7, 2008 yeah abs() i just seen that Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485883 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. Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485884 Share on other sites More sharing options...
Naez Posted March 7, 2008 Share Posted March 7, 2008 echo -abs(500); Link to comment https://forums.phpfreaks.com/topic/94853-from-negative-to-positive/#findComment-485887 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.