我需要将NSFileSystemSize转换为千兆字节。
NSDictionary * fsAttributes = [ [NSFileManager defaultManager]
fileSystemAttributesAtPath:NSTemporaryDirectory()];
NSNumber *totalSize = [fsAttributes objectForKey:NSFileSystemSize];
NSString *sizeInGB = [NSString stringWithFormat:@"\n\n %3.2f GB",[totalSize floatValue] / 107374824];
//returns 69.86 GB
为什么它不能以8.0GB的速度返回?
答案 0 :(得分:2)
作为一个尼特,1024 * 1024 * 1024
是1073741824
,而不是107374824
(你在千万个地方错过了一个。)
答案 1 :(得分:0)
- (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;
}