Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Posts
    454
    Thanks
    0
    Thanked 20 Times in 20 Posts

    bit vs. byte calculator readable problem

    Ok I finally found a 'file size calculator' built in javascript.
    Code:
    <!doctype html public "-//w3c//dtd html 3.2//en">
    
    <html>
    
    <head>
    <title>(Type a title for your page here)</title>
    </head>
    
    <body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
    
    File Size Calculator:
    <SCRIPT type="text/javascript"><!--
    var lastSource = 'bytes';
    
    function updateCalc( source )
    {
    	var oVal=0, val=0;
    	if( typeof(source) == 'undefined' || source == "" )
    		source = lastSource;
    	else
    		lastSource = source;
    
    	eval("oVal = document.Calc."+source+".value;");
    	val = oVal;
    	switch(source) {
    	case "kB":
    		val *= 1000;
    		break;
    	case "KiB":
    		val *= 1024;
    		break;
    	case "MB":
    		val *= 1000000;
    		break;
    	case "MiB":
    		val *= 1048576;
    		break;
    	case "GB":
    		val *= 1e9;
    		break;
    	case "GiB":
    		val *= 1073741824;
    		break;
    	case "TB":
    		val *= 1e12;
    		break;
    	case "TiB":
    		val *= 1099511627776;
    		break;
    	case "PB":
    		val *= 1e15;
    		break;
    	case "PiB":
    		val *= 1125899906842624;
    		break;
    	case "EB":
    		val *= 1e18;
    		break;
    	case "EiB":
    		val *= 1152921504606846976;
    		break;
    	case "bits":
    		val /= 8;
    		break;
    	}
    
    	document.Calc.bytes.value = Math.ceil(val);
    	if( source == "bits" )
    		document.Calc.bits.value = oVal;
    	else
    		document.Calc.bits.value = Math.ceil(val)*8;
    
    	if(document.Calc.roundValues.checked) {
    		if(document.Calc.roundUp.checked) {
    			document.Calc.kB.value  = Math.ceil(val/1000);
    			document.Calc.KiB.value = Math.ceil(val/1024);
    			document.Calc.MB.value  = Math.ceil(val/1000000);
    			document.Calc.MiB.value = Math.ceil(val/1048576);
    			document.Calc.GB.value  = Math.ceil(val/1e9);
    			document.Calc.GiB.value = Math.ceil(val/1073741824);
    			document.Calc.TB.value  = Math.ceil(val/1e12);
    			document.Calc.TiB.value = Math.ceil(val/1099511627776);
    			document.Calc.PB.value  = Math.ceil(val/1e15);
    			document.Calc.PiB.value = Math.ceil(val/1125899906842624);
    			document.Calc.EB.value  = Math.ceil(val/1e18);
    			document.Calc.EiB.value = Math.ceil(val/1152921504606846976);
    		}
    		else {
    			document.Calc.kB.value  = Math.round(val/10)/100;
    			document.Calc.KiB.value = Math.round(val/10.24)/100;
    			document.Calc.MB.value  = Math.round(val/10000)/100;
    			document.Calc.MiB.value = Math.round(val/10485.76)/100;
    			document.Calc.GB.value  = Math.round(val/10000000)/100;
    			document.Calc.GiB.value = Math.round(val/10737418.24)/100;
    			document.Calc.TB.value  = Math.round(val/1e10)/100;
    			document.Calc.TiB.value = Math.round(val/10995116277.76)/100;
    			document.Calc.PB.value  = Math.round(val/1e13)/100;
    			document.Calc.PiB.value = Math.round(val/11258999068426.24)/100;
    			document.Calc.EB.value  = Math.round(val/1e16)/100;
    			document.Calc.EiB.value = Math.round(val/11529215046068469.76)/100;
    		}
    	}
    	else {
    		if( document.Calc.roundUp.checked )
    			document.Calc.roundUp.checked=false;
    
    		document.Calc.kB.value  = val/1000;
    		document.Calc.KiB.value = val/1024;
    		document.Calc.MB.value  = val/1000000;
    		document.Calc.MiB.value = val/1048576;
    		document.Calc.GB.value  = val/1e9;
    		document.Calc.GiB.value = val/1073741824;
    		document.Calc.TB.value  = val/1e12;
    		document.Calc.TiB.value = val/1099511627776;
    		document.Calc.PB.value  = val/1e15;
    		document.Calc.PiB.value = val/1125899906842624;
    		document.Calc.EB.value  = val/1e18;
    		document.Calc.EiB.value = val/1152921504606846976;
    	}
    }
    
    function toggleRoundUp()
    {
    	if( document.Calc.roundUp.checked )
    		document.Calc.roundValues.checked=true;
    	updateCalc();
    }
    // -->
    </SCRIPT>
    
    
    <DIV class="escWrapper">
    <FORM name="Calc" action="javascript:return false" onSubmit="updateCalc();return false;">
    
    <TABLE border=0 cellspacing=0 cellpadding=1 style="margin-left: auto; margin-right: auto">
    <TR>
    <TD class="Label"><LABEL for="bytes">Bytes:</LABEL></TD><TD><INPUT type="text" name="bytes" value="1000000" size=20 onChange="updateCalc('bytes')" id="bytes"></TD>
    <TD class="pow">10<SUP>0</SUP></TD>
    <TD class="Label"><LABEL for="bits">bits:</LABEL></TD><TD><INPUT type="text" name="bits" value="1000000" size=20 onChange="updateCalc('bits')" id="bits"></TD>
    <TD class="pow">2<SUP>-3</SUP></TD>
    </TR>
    <TR>
    <TD class="Label"><LABEL for="kB">kiloBytes (kB):</LABEL></TD><TD><INPUT type="text" name="kB" value="?" size=20 onChange="updateCalc('kB')" id="kB"></TD>
    <TD class="pow">10<SUP>3</SUP></TD>
    
    <TD class="Label"><LABEL for="KiB">kibiBytes (KiB):</LABEL></TD><TD><INPUT type="text" name="KiB" value="?" size=20 onChange="updateCalc('KiB')" id="KiB"></TD>
    <TD class="pow">2<SUP>10</SUP></TD>
    </TR>
    <TR>
    <TD class="Label"><LABEL for="MB">megaBytes (MB):</LABEL></TD><TD><INPUT type="text" name="MB" value="?" size=20 onChange="updateCalc('MB')" id="MB"></TD>
    <TD class="pow">10<SUP>6</SUP></TD>
    <TD class="Label"><LABEL for="MiB">mebiBytes (MiB):</LABEL></TD><TD><INPUT type="text" name="MiB" value="?" size=20 onChange="updateCalc('MiB')" id="MiB"></TD>
    <TD class="pow">2<SUP>20</SUP></TD>
    
    </TR>
    <TR>
    <TD class="Label"><LABEL for="GB">gigaBytes (GB):</LABEL></TD><TD><INPUT type="text" name="GB" value="?" size=20 onChange="updateCalc('GB')" id="GB"></TD>
    <TD class="pow">10<SUP>9</SUP></TD>
    <TD class="Label"><LABEL for="GiB">gibiBytes (GiB):</LABEL></TD><TD><INPUT type="text" name="GiB" value="?" size=20 onChange="updateCalc('GiB')" id="GiB"></TD>
    <TD class="pow">2<SUP>30</SUP></TD>
    </TR>
    <TR>
    <TD class="Label"><LABEL for="TB">teraBytes (TB):</LABEL></TD><TD><INPUT type="text" name="TB" value="?" size=20 onChange="updateCalc('TB')" id="TB"></TD>
    <TD class="pow">10<SUP>12</SUP></TD>
    
    <TD class="Label"><LABEL for="TiB">tebiBytes (TiB):</LABEL></TD><TD><INPUT type="text" name="TiB" value="?" size=20 onChange="updateCalc('TiB')" id="TiB"></TD>
    <TD class="pow">2<SUP>40</SUP></TD>
    </TR>
    <TR>
    <TD class="Label"><LABEL for="PB">petaBytes (PB):</LABEL></TD><TD><INPUT type="text" name="PB" value="?" size=20 onChange="updateCalc('PB')" id="PB"></TD>
    <TD class="pow">10<SUP>15</SUP></TD>
    <TD class="Label"><LABEL for="PiB">pebiBytes (PiB):</LABEL></TD><TD><INPUT type="text" name="PiB" value="?" size=20 onChange="updateCalc('PiB')" id="PiB"></TD>
    <TD class="pow">2<SUP>50</SUP></TD>
    
    </TR>
    <TR>
    <TD class="Label"><LABEL for="EB">exaBytes (EB):</LABEL></TD><TD><INPUT type="text" name="EB" value="?" size=20 onChange="updateCalc('EB')" id="EB"></TD>
    <TD class="pow">10<SUP>18</SUP></TD>
    <TD class="Label"><LABEL for="EiB">exbiBytes (EiB):</LABEL></TD><TD><INPUT type="text" name="EiB" value="?" size=20 onChange="updateCalc('EiB')" id="EiB"></TD>
    <TD class="pow">2<SUP>60</SUP></TD>
    </TR>
    
    <TR style="text-align: right">
    <TD><INPUT type="submit" value="Update"></TD><TD><LABEL><INPUT type="checkbox" checked="checked" name="roundValues" onClick="updateCalc()">Round Values</LABEL></TD><TD></TD><TD><LABEL><INPUT type="checkbox" name="roundUp" onClick="toggleRoundUp()">Round Up</LABEL></TD>
    
    </TR>
    </TABLE>
    </FORM>
    </DIV>
    
    </body>
    
    </html>
    The problem is that the above calculator answers do not match with either of the following php calculators for readable byte info.

    PHP Code:
    function FormatFileSize($size$precision 2){
    $base log($size) / log(1024);

    $suffixes = array('''k''M''G''T');
    return 
    round(pow(1024$base floor($base)), $precision) . $suffixes[floor($base)];
    }



    function 
    format_size($size) {
      if (
    $size 1024) {
        return 
    $size ' bytes';
      }
      else {
        
    $size round($size 10242);
        
    $suffix 'KB';
        if (
    $size >= 1024) {
          
    $size round($size 10242);
          
    $suffix 'MB';
        }
        return 
    $size ' ' $suffix;
      }

    when tested using 1M the js calculator calcs:
    1050000 bits for 1M rounded up
    100000 bytes for 1M rounded up

    but when using the php scripts
    1050000 returns 1M
    100000 returns 97.66k

    so what's right and what's not?
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,627
    Thanks
    0
    Thanked 648 Times in 638 Posts
    The JavaScript equivalent of that PHP script is:

    Code:
    function FormatFileSize($size, $precision){ 
    if ($precision === undefined) $precision = 2;
    $base = Math.log($size) / Math.log(1024); 
    
    $suffixes = ['', 'k', 'M', 'G', 'T']; 
    return Math.round(Math.pow(1024, $base - Math.floor($base)), $precision) . $suffixes[Math.floor($base)]; 
    } 
    
    
    
    function format_size($size) { 
      if ($size < 1024) { 
        return $size . ' bytes'; 
      } 
      else { 
        $size = Math.round($size / 1024, 2); 
        $suffix = 'KB'; 
        if ($size >= 1024) { 
          $size = Math.round($size / 1024, 2); 
          $suffix = 'MB'; 
        } 
        return $size . ' ' . $suffix; 
      } 
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Posts
    454
    Thanks
    0
    Thanked 20 Times in 20 Posts
    Thanks felgall, what I was looking for was the php equivalent to the javascript. While most forms and file systems use filesize function to make the size readable I wanted to use the javascript as a popup to form element so I could enter say 5M and the bytes would go into the form element. But I need the php readable filesize function to show the correct size when when pulling byte info. Hmmm...php for filesize info function js script for form input which would send bytes info to form element to be used for settings. Checks and balances...both should reflect the same. I guess I explained it right.
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •