-
- 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;
- }
Copy code
2. js implementation code
-
- 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;
- }
- ?>
Copy code
Note: in the code Keep 2 decimal places.
|