Jump to content

Is my php math code right?

hyperj123

I'm having a brain fart with math at the moment but this just doesn't seem correct to me. I am calculating disk space of a user. I want the bar to represent the percent of usage. 

$curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curl, CURLOPT_HEADER,0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($curl, CURLOPT_USERPWD, $username.":".$password);
    curl_setopt($curl, CURLOPT_URL, $query);
    $result = curl_exec($curl);
    curl_close($curl); 

    $xml = simpleXML_load_string($result);

    $df = $xml->data[0]->_count; // used MB
    $ds = $xml->data[0]->_max; // max MB
    $du = $ds - $df; // free MB
	$avail = round($ds/$df *.1,2);
    if ($ds > 0) $perc = number_format(100 * $du / $ds, 2); 
	else $perc = 0;
    $color = 'green';
    if ($perc < 50) $color = 'blue';
    if ($perc < 90) $color = 'red';
    echo '<li style="font-weight:bold;padding:5px 15px;border-radius:4px;
	-moz-border-radius:4px;-webkit-border-radius:4px;
	background-color:#182227;margin-left:13px;color:#afc5cf;">'
        .'Free disk space'
        .'<div style="border:1px solid #ccc;width:100%;margin:2px 5px 2px 0;padding:1px">'
        .'<div style="width:'.$avail.'%;background-color:'.$color.';height:6px"></div></div>'
        .$du.' of '.$ds.' MB free'.'</li>'
		.$df.': Used Space<br>'
		.$ds.': Maximum Storage<br>'
		.$du.': Free Space<br>'
		.$avail.': Available Percentage?<br>';

sgbNkSv.png

Link to comment
Share on other sites

Link to post
Share on other sites

Mod's please move I just noticed I posted in the wrong section. Sorry :(

Link to comment
Share on other sites

Link to post
Share on other sites

we learned this after learning multiplication and addition ...  multiply diagonally, divide

 

100% .....  max MB

 ? %  ......  count MB

 

? %  =  (count MB * 100 ) / max MB

 

4025.3 * 100 / 128000 = 3.1447 %  = 3.145% = 3.15% (depending on how much you want to round, i'd go with 3 decimals)

free space percentage = 100% - 3.15% = 96.85%

 

For lots of these calculations you can optimize by multiplying with 10000 or 100000 (if you want 3 decimals precision) instead of 100 and use ceil() or floor() along with intval or some other on the result so that you won't work with float numbers until the moment you display them on screen.

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 1/28/2017 at 4:37 AM, mariushm said:

we learned this after learning multiplication and addition ...  multiply diagonally, divide

 

100% .....  max MB

 ? %  ......  count MB

 

? %  =  (count MB * 100 ) / max MB

 

4025.3 * 100 / 128000 = 3.1447 %  = 3.145% = 3.15% (depending on how much you want to round, i'd go with 3 decimals)

free space percentage = 100% - 3.15% = 96.85%

 

For lots of these calculations you can optimize by multiplying with 10000 or 100000 (if you want 3 decimals precision) instead of 100 and use ceil() or floor() along with intval or some other on the result so that you won't work with float numbers until the moment you display them on screen.

 

Thank you. I had a huge brain fart while configuring this. I got it working now.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×