Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Byte conversion problem 1

Status
Not open for further replies.

dle46163

IS-IT--Management
Jul 9, 2004
81
US
Hi All,

I found a small script for byte conversion (to Mb, Gb, etc..). It seems to work well, however when calling the function as so:

Case 1: byte_convert('1070900686');
Case 2: byte_convert('1073741824');

My results are as follows:

Case 1. 1,021.29MB
Case 2. 1.00GB

Clearly in case 1 the number of bytes input is less than case 2. However, the output shows more than a gig. My questions are why is it returning more than a gig in case 1 and if the result in case 1 were really true, why isn't the function returning the value as GB instead of MB? Any help would be great! See function code below:


function byte_convert($bytes){
$size = $bytes / 1024;
if($size < 1024) {
$size = number_format($size, 2);
$size .= 'KB';
}
else {
if($size / 1024 < 1024){
$size = number_format($size / 1024, 2);
$size .= 'MB';
}
else if ($size / 1024 / 1024 < 1024){
$size = number_format($size / 1024 / 1024, 2);
$size .= 'GB';
}
}
return $size;
}







 
The function is working properly.

What most people don't realize is that 1 GB = 1024 MB, not the straight 1000 MB people understand it to be. Same thing goes for the MB and KB (ie 1 MB = 1024 KB, 1 KB = 1024 Bytes).

Now - why is that the case? I don't remember.
 
Thanks, can't believe I forgot that! It all makes sense now..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top