系统转换方法,自动输出合适的单位
NSDictionary *fileInfo = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
NSString *fileSize = [NSByteCountFormatter stringFromByteCount:[fileInfo[NSFileSize] longLongValue] countStyle:NSByteCountFormatterCountStyleFile];
自定义转换方法,按需求选择单位
- (NSString *)transformedValue:(id)value {
CGFloat convertedValue = [value doubleValue];
NSInteger 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]];
}