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;
}
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;
}