Jump to content

Finding the difference between 2 numbers?


Texan78
Go to solution Solved by Psycho,

Recommended Posts

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. 

 

2sxj.png

Link to comment
Share on other sites

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 by davidannis
Link to comment
Share on other sites

  • Solution

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

 

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. 

Link to comment
Share on other sites

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 by Texan78
Link to comment
Share on other sites

 

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

Link to comment
Share on other sites

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);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.