php和js实现转换文件大小为人性化可读的方式
时间:2021-07-01 10:21:17
帮助过:21人阅读
- 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位小数。 |