shadiadiph Posted January 27, 2009 Share Posted January 27, 2009 MM odd question here if I have two numbers that have values such as $current = 1.4567 $last = 1.4698 how do i find out the difference between the two numbers sometimes the values might be the otherway round the one above should produce a negative but if $current was 1.4824 the difference would be positive how can i determine this positive negative difference? Quote Link to comment https://forums.phpfreaks.com/topic/142614-solved-finding-the-difference-between-2-numbers/ Share on other sites More sharing options...
corbin Posted January 27, 2009 Share Posted January 27, 2009 $current-$last; Or if you wanted a positive number: abs($current-$last); Quote Link to comment https://forums.phpfreaks.com/topic/142614-solved-finding-the-difference-between-2-numbers/#findComment-747679 Share on other sites More sharing options...
shadiadiph Posted February 5, 2009 Author Share Posted February 5, 2009 i figured it out $new =$current-$last gives me a result if negative it shows -2.16 but if the result was positive and +2.16 it only shows 2.16 will your way display it as +2.16 if positive and -2.16 if negative? Quote Link to comment https://forums.phpfreaks.com/topic/142614-solved-finding-the-difference-between-2-numbers/#findComment-755060 Share on other sites More sharing options...
corbin Posted February 5, 2009 Share Posted February 5, 2009 if($num > 0) $num = '+' . $num; Note: When ever you do anything to that number, such as subtract or add something to it, you will need to reprepend the +. Quote Link to comment https://forums.phpfreaks.com/topic/142614-solved-finding-the-difference-between-2-numbers/#findComment-755457 Share on other sites More sharing options...
Mark Baker Posted February 5, 2009 Share Posted February 5, 2009 Better to use sprintf() to format the number at the point when you need to display it. echo sprintf('%+f',$new); Quote Link to comment https://forums.phpfreaks.com/topic/142614-solved-finding-the-difference-between-2-numbers/#findComment-755474 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.