function my_formatSize($size){$size = doubleval($size);$rank =0;$rankchar ='Bytes';while($size>1024){ $size = $size/1024; $rank ; } if($rank==1){ $rankchar="KB"; } else if($rank==2){ $rankchar="MB"; } else if($rank==3){ $rankchar="GB"; } $size = number_format($size, 2, '.', ''); return "".$size." ".$rankchar; } 复制代码 二、js实现代码 function my_formatSize($size){ var size = parseFloat($size); var rank =0; var rankchar ='Bytes'; while(size>1024){ size = size/1024; rank ; } if(rank==1){ rankchar="KB"; } else if(rank==2){ rankchar="MB"; } else if(rank==3){ rankchar="GB"; } return size.toFixed(2) " " rankchar; } ?> 复制代码 注:代码中保留2位小数。