2010-05-15 85 views
0

我需要將NSFileSystemSize轉換爲千兆字節。將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的?

回答

2

作爲尼特,1024 * 1024 * 10241073741824,不107374824(你在千位缺少1)

+0

尼斯漁獲物。我實際上去了: NSString * sizeInGB = [NSString stringWithFormat:@「\ n \ n%.2f GB」,[totalSize floatValue]/1024/1024/1024];似乎工作正常。 – WrightsCS 2010-05-15 05:58:21

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; 
}