Convert NSFileSystemSize to Gigabytes
I need to convert NSFileSystemSize to Gigabytes.
NSDictionary * fsAttributes = [ [NSFileManager defaultManager]
fileSystemAttributesAtPath:NSTemporaryDirectory()];
NSNumber *totalSize = [fsAttributes objectForKey:NS开发者_JAVA百科FileSystemSize];
NSString *sizeInGB = [NSString stringWithFormat:@"\n\n %3.2f GB",[totalSize floatValue] / 107374824];
//returns 69.86 GB
any ideas why it doesnt return at leat 8.0GB's?
As a nit, 1024 * 1024 * 1024
is 1073741824
, not 107374824
(you're missing a 1 in the thousands place.)
- (NSString *)formattedFileSize:(unsigned long long)size
{
NSString *formattedStr = nil;
if (size == 0)
formattedStr = @"Empty";
else
if (size > 0 && size < 1024)
formattedStr = [NSString stringWithFormat:@"%qu bytes", size];
else
if (size >= 1024 && size < pow(1024, 2))
formattedStr = [NSString stringWithFormat:@"%.1f KB", (size / 1024.)];
else
if (size >= pow(1024, 2) && size < pow(1024, 3))
formattedStr = [NSString stringWithFormat:@"%.2f MB", (size / pow(1024, 2))];
else
if (size >= pow(1024, 3))
formattedStr = [NSString stringWithFormat:@"%.3f GB", (size / pow(1024, 3))];
return formattedStr;
}
精彩评论