Skip to content

Instantly share code, notes, and snippets.

@yangg
Created September 26, 2011 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yangg/1241850 to your computer and use it in GitHub Desktop.
Save yangg/1241850 to your computer and use it in GitHub Desktop.
Format Filesize
// http://www.cnblogs.com/yaob/archive/2011/05/14/2046013.html
public string formatSize(long size)
{
if (size == 0) return "0";
string[] sizetext = new string[] { "B", "KB", "MB", "GB", "TB", "PB" };
int i = (int)Math.Floor(Math.Log(size, 1024));
return Math.Round(size / Math.Pow(1024, i), 2).ToString() + " " + sizetext[i];
}
function formatSize(size) {
var sizetext = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
i = Math.floor(Math.log(size)/Math.log(1024));
return (size/Math.pow(1024, i)).toFixed(2) + ' ' + sizetext[i];
}
function format($size) {
$sizetext = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
return round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$sizetext[$i];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment