如何在 Cocoa 中正确获取文件大小并将其转换为 MB、GB? [英] How to get file size properly and convert it to MB, GB in Cocoa?

查看:20
本文介绍了如何在 Cocoa 中正确获取文件大小并将其转换为 MB、GB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
ObjC/Cocoa 类,用于将大小转换为人类可读的字符串?

我是 Cocoa 的新手.我正在尝试正确获取文件夹文件的大小.如果小于 1 GB 或以 GB 显示,则以 MB 为单位显示.

I'm new in Cocoa. I'm trying to get size of folder files properly. And display it in MB if it less 1 GB , or in GB.

我希望它的显示方式是在一个点后四舍五入.

The way I want it to display is rounded with one number after point.

示例5.5 MB 如果大于 1000 > 1.1 GB

Example 5.5 MB if it is more than 1000 > 1.1 GB

我正在尝试使用这个

 unsigned  long long size= ([[[NSFileManager defaultManager] attributesOfItemAtPath:fullPath error:nil] fileSize]);

但我无法正确转换数字并按照我的意愿显示它.

But I can't a way properly convert number, and display it , as I want.

谢谢.

推荐答案

用于将文件大小转换为 MB、Gb 使用以下函数

For converting file size to MB, Gb use below function

- (id)transformedValue:(id)value
{
    
    double convertedValue = [value doubleValue];
    int multiplyFactor = 0;
    
    NSArray *tokens = @[@"bytes",@"KB",@"MB",@"GB",@"TB",@"PB", @"EB", @"ZB", @"YB"];
    
    while (convertedValue > 1024) {
        convertedValue /= 1024;
        multiplyFactor++;
    }
    
    return [NSString stringWithFormat:@"%4.2f %@",convertedValue, tokens[multiplyFactor]];
}

您也可以使用 NSByteCountFormatter 班级.适用于 iOS 6.0/OS X v10.8 及更高版本.

You can also use NSByteCountFormatter class. Available in iOS 6.0 / OS X v10.8 and later.

[NSByteCountFormatter stringFromByteCount:1999 countStyle:NSByteCountFormatterCountStyleFile];

您可以在 countStyle 中使用 NSByteCountFormatterCountStyleFileNSByteCountFormatterCountStyleMemoryNSByteCountFormatterCountStyleDecimalNSByteCountFormatterCountStyleBinary.

You can use NSByteCountFormatterCountStyleFile, NSByteCountFormatterCountStyleMemory, NSByteCountFormatterCountStyleDecimal or NSByteCountFormatterCountStyleBinary in countStyle.

NSByteCountFormatterCountStyleFile:指定文件或存储字节数的显示.实际行为是特定于平台;在 OS X 10.8 上,这使用十进制样式,但是可能会随着时间而改变.

NSByteCountFormatterCountStyleFile: Specifies display of file or storage byte counts. The actual behavior for this is platform-specific; on OS X 10.8, this uses the decimal style, but that may change over time.

NSByteCountFormatterCountStyleMemory:指定显示内存字节数.这的实际行为是特定于平台的;在操作系统上X 10.8,这使用二进制样式,但可能会随着时间的推移而改变.

NSByteCountFormatterCountStyleMemory: Specifies display of memory byte counts. The actual behavior for this is platform-specific; on OS X 10.8, this uses the binary style, but that may change over time.

NSByteCountFormatterCountStyleDecimal:明确指定KB的字节数,1000字节显示为1 KB

NSByteCountFormatterCountStyleDecimal: Specifies the number of bytes for KB explicitly, 1000 bytes are shown as 1 KB

NSByteCountFormatterCountStyleBinary:明确指定KB的字节数,1024字节显示为1KB

NSByteCountFormatterCountStyleBinary: Specifies the number of bytes for KB explicitly, 1024 bytes are shown as 1 KB

这篇关于如何在 Cocoa 中正确获取文件大小并将其转换为 MB、GB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆