Texan78 Posted April 9, 2014 Share Posted April 9, 2014 Hello guys. I am looking for a better solution for what I have. I have done some google searches but my results didn't return much as most was related to date. I have this small script I have thrown together that is using data that I parse using SimpleXML. If you look at the picture there is a normal level. That is a set level. The current level is of course the current level. I need to find the difference between the two. If the current is less than the normal level I am trying to get it to display the difference with - and if the current is more than the normal level show +. What I have at the moment works ok but it doesn't show if it is -/+ and it doesn't figure in the dec point. Is there a better way to do this than what I have and also a way to pull the percentage as well? I.E. Current Level is X% of the Normal Level or in this case X% Full. Here is the code I am currently using to get the difference which is rather straight forward. $LRH_departure = $LRH_bankfull-$LRH_current; Here is a screenshot of the overall output. Note, this is still in development so it is very rough looking at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/287644-finding-the-difference-between-2-numbers/ Share on other sites More sharing options...
davidannis Posted April 9, 2014 Share Posted April 9, 2014 (edited) What you have should calculate plus or minus and decimals. I suspect that the issue is in how you output the result but need to see more code to know. If you just echo $LRH_departure; right after it is calculated you can see what that looks like. Edited April 9, 2014 by davidannis Quote Link to comment https://forums.phpfreaks.com/topic/287644-finding-the-difference-between-2-numbers/#findComment-1475553 Share on other sites More sharing options...
Texan78 Posted April 9, 2014 Author Share Posted April 9, 2014 Thanks, the output you see in the screenshot is exactly how results are outputted using the echo as such. <td> <?php echo $LRH_departure; ?> Ft</td> Quote Link to comment https://forums.phpfreaks.com/topic/287644-finding-the-difference-between-2-numbers/#findComment-1475555 Share on other sites More sharing options...
Solution Psycho Posted April 9, 2014 Solution Share Posted April 9, 2014 What are the actual values you are working with? Are you using values such as '428.13 ft' or is is just '428.13'? I will assume the former. function calcLevel($current, $normal) { //Get float values from strings $current = floatval($current); $normal = floatval($normal); //Calculate the difference $calcLevel = $current - $normal; //Format difference to two decimal places and add 'Ft' $calcLevelStr = (string) number_format($calcLevel, 2) . ' Ft'; //If vlaue is positive add a + to beginning if($calcLevel>0) { $calcLevelStr = '+'.$calcLevelStr; } //Return the calculated formatted value return $calcLevelStr; } echo calcLevel('445.5 Ft', '428.13 Ft'); //Output +17.37 F echo calcLevel('405.5 Ft', '428.13 Ft'); //Output -22.63 Ft Quote Link to comment https://forums.phpfreaks.com/topic/287644-finding-the-difference-between-2-numbers/#findComment-1475557 Share on other sites More sharing options...
.josh Posted April 9, 2014 Share Posted April 9, 2014 If $LRH_departure is showing rounded number then you must be rounding the number other vars before you do the calculation. Simple exercise: <?php $LRH_bankfull= 435.5; $LRH_current= 428.13; $LRH_departure = $LRH_bankfull-$LRH_current; echo $LRH_departure; // output: 7.37 php doesn't (necessarily) round by itself (until you get into much larger numbers after the decimal point), so you definitely have something that's rounding your numbers at some point. Quote Link to comment https://forums.phpfreaks.com/topic/287644-finding-the-difference-between-2-numbers/#findComment-1475559 Share on other sites More sharing options...
Texan78 Posted April 9, 2014 Author Share Posted April 9, 2014 What are the actual values you are working with? Are you using values such as '428.13 ft' or is is just '428.13'? I will assume the former. function calcLevel($current, $normal) { //Get float values from strings $current = floatval($current); $normal = floatval($normal); //Calculate the difference $calcLevel = $current - $normal; //Format difference to two decimal places and add 'Ft' $calcLevelStr = (string) number_format($calcLevel, 2) . ' Ft'; //If vlaue is positive add a + to beginning if($calcLevel>0) { $calcLevelStr = '+'.$calcLevelStr; } //Return the calculated formatted value return $calcLevelStr; } echo calcLevel('445.5 Ft', '428.13 Ft'); //Output +17.37 F echo calcLevel('405.5 Ft', '428.13 Ft'); //Output -22.63 Ft Yes it is just the former. The Ft is added in the HTML after the echo. I am assuming with the "echo calcLevel" you replace the hard coded numbers with the variables? That looks exactly what I am looking for but not sure how to output that into the table. Quote Link to comment https://forums.phpfreaks.com/topic/287644-finding-the-difference-between-2-numbers/#findComment-1475570 Share on other sites More sharing options...
Texan78 Posted April 9, 2014 Author Share Posted April 9, 2014 (edited) If $LRH_departure is showing rounded number then you must be rounding the number other vars before you do the calculation. Simple exercise: <?php $LRH_bankfull= 435.5; $LRH_current= 428.13; $LRH_departure = $LRH_bankfull-$LRH_current; echo $LRH_departure; // output: 7.37 php doesn't (necessarily) round by itself (until you get into much larger numbers after the decimal point), so you definitely have something that's rounding your numbers at some point. That is pretty much the gist of it how it is. The data is created from parsing the XML file via SimpleXML. Here is a small example for one of them. SimpleXML feed parsing. //Parse Lake Ray Hubbard XML Data $site = simplexml_load_file($LRH_data);{ $LRH_bankfull = $site->sigstages[0]->bankfull; $LRH_current = $site->observed->datum[0]->primary; $LRH_vaild = $site->observed->datum[0]->valid; $LRH_updated = DATE("D, M d, g:i a", STRTOTIME($LRH_vaild)); } Then from there I take the data from those variables to get the departure. //Lets calculate the lake depatures from full pool $LRH_departure = $LRH_bankfull-$LRH_current; Then the new variable to get the departure is echo'd in the table. <td> <?php echo $LRH_departure;; ?> Ft</td> Pretty simple and straight forward but, yes it is rather strange it is rounding the number. Edited April 9, 2014 by Texan78 Quote Link to comment https://forums.phpfreaks.com/topic/287644-finding-the-difference-between-2-numbers/#findComment-1475574 Share on other sites More sharing options...
Texan78 Posted April 10, 2014 Author Share Posted April 10, 2014 What are the actual values you are working with? Are you using values such as '428.13 ft' or is is just '428.13'? I will assume the former. function calcLevel($current, $normal) { //Get float values from strings $current = floatval($current); $normal = floatval($normal); //Calculate the difference $calcLevel = $current - $normal; //Format difference to two decimal places and add 'Ft' $calcLevelStr = (string) number_format($calcLevel, 2) . ' Ft'; //If vlaue is positive add a + to beginning if($calcLevel>0) { $calcLevelStr = '+'.$calcLevelStr; } //Return the calculated formatted value return $calcLevelStr; } echo calcLevel('445.5 Ft', '428.13 Ft'); //Output +17.37 F echo calcLevel('405.5 Ft', '428.13 Ft'); //Output -22.63 Ft Ok, I got it sorted with the help of this code. Can anyone point me in the direction for a good tutorial for finding the % between the two numbers now? -Thanks Quote Link to comment https://forums.phpfreaks.com/topic/287644-finding-the-difference-between-2-numbers/#findComment-1475609 Share on other sites More sharing options...
Texan78 Posted April 10, 2014 Author Share Posted April 10, 2014 Ok well I found a way to get the percentage. This is what I did. Pretty simple and compact. Not sure if there is a better way or not. $val1 = $LRH_current; $val2 = $LRH_bankfull; $LRH_prec = ( $val1 / $val2) * 100; // 1 digit after the decimal point $LRH_prec = round($LRH_prec, 1); Quote Link to comment https://forums.phpfreaks.com/topic/287644-finding-the-difference-between-2-numbers/#findComment-1475624 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.