Created
          November 12, 2009 16:45 
        
      - 
      
- 
        Save seven1m/233053 to your computer and use it in GitHub Desktop. 
    matching ruby and javascript methods for formatting bytes in human readable format
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function number_to_human_size(size) { | |
| if(size < 1024) | |
| return size + ' bytes'; | |
| else if(size < 1024.0 * 1024.0) | |
| return (size / 1024.0).toFixed(1) + ' KiB' | |
| else if(size < 1024.0 * 1024.0 * 1024.0) | |
| return (size / 1024.0 / 1024.0).toFixed(1) + ' MiB' | |
| else | |
| return (size / 1024.0 / 1024.0 / 1024.0).toFixed(1) + ' GiB'; | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | def number_to_human_size(size) | |
| if size < 1024 | |
| "#{size} bytes" | |
| elsif size < 1024.0 * 1024.0 | |
| "%.01f KiB" % (size / 1024.0) | |
| elsif size < 1024.0 * 1024.0 * 1024.0 | |
| "%.01f MiB" % (size / 1024.0 / 1024.0) | |
| else | |
| "%.01f GiB" % (size / 1024.0 / 1024.0 / 1024.0) | |
| end | |
| end | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment