字节转换

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

}

posted @ 2012-07-08 20:52  ValeTu  阅读(269)  评论(0编辑  收藏  举报