Skip to content

Instantly share code, notes, and snippets.

@CaliosD
Created March 21, 2016 02:29
Show Gist options
  • Save CaliosD/434c6e17f9752df710bb to your computer and use it in GitHub Desktop.
Save CaliosD/434c6e17f9752df710bb to your computer and use it in GitHub Desktop.
求文件大小
- (NSString *)formattedFileSize:(unsignedlonglong)size
{
NSString *formattedStr = nil;
if (size == 0)
formattedStr = @"Empty";
else
if (size > 0 && size < 1024)
formattedStr = [NSStringstringWithFormat:@"%qu bytes", size];
// Lilac-Annotation: %qu: Unsigned 64-bit integer (unsigned long long)
else
if (size >= 1024 && size < pow(1024, 2))
formattedStr = [NSStringstringWithFormat:@"%.1f KB", (size / 1024.)];
else
if (size >= pow(1024, 2) && size < pow(1024, 3))
formattedStr = [NSStringstringWithFormat:@"%.2f MB", (size / pow(1024, 2))];
// Lilac-Annotation: pow(x,y),计算x的y次幂
else
if (size >= pow(1024, 3))
formattedStr = [NSStringstringWithFormat:@"%.3f GB", (size / pow(1024, 3))];
return formattedStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment