代码之家  ›  专栏  ›  技术社区  ›  WrightsCS

将NSFileSystemSize转换为千兆字节

  •  0
  • WrightsCS  · 技术社区  · 14 年前

    我需要把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
    

    2 回复  |  直到 12 年前
        1
  •  2
  •   fbrereto    14 年前

    作为一个笨蛋, 1024 * 1024 * 1024 1073741824 107374824 (你错过了千分之一。)

        2
  •  0
  •   Hugo the Lee    12 年前
    - (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;
    }