Skip to content

Instantly share code, notes, and snippets.

@vizhen
Created February 10, 2014 12:58
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 vizhen/8915455 to your computer and use it in GitHub Desktop.
Save vizhen/8915455 to your computer and use it in GitHub Desktop.
格式化文件大小
/**
* 文件大小格式转换
* @param fileS 文件大小
* @return 文件大小
*/
public static String FormatFileSize(long fileS)
{
// 转换文件大小
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024)
{
fileSizeString = df.format((double) fileS) + "B";
}
else if (fileS < 1048576)
{
fileSizeString = df.format((double) fileS / 1024) + "KB";
}
else if (fileS < 1073741824)
{
fileSizeString = df.format((double) fileS / 1048576) + "MB";
}
else
{
fileSizeString = df.format((double) fileS / 1073741824) + "GB";
}
return fileSizeString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment