官方将文件size转换为MGB字符串

来源:互联网 发布:焦作市青峰网络 编辑:程序博客网 时间:2024/04/27 13:52
//功能:将字节大小(Kb)转换为Gb或Mb
//参数:KbSizestr: 以Kb为单位的size字符串
//返回值:转换为Gb或Mb的字符串
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;
}
原创粉丝点击