5

Is there build in methods for formatting file sizes in Objective C? Or may be you could suggest me some library/source code/etc.?

What I mean is you have some file size that should be displayed something like this depending on given size:

  • 1234 kb
  • 1,2 mb
  • etc..

Thanks in advance

Pan.Krutilkin
  • 91
  • 3
  • 5

3 Answers3

13

This one solves the problem quite elegantly:

[NSByteCountFormatter stringFromByteCount:countStyle:]

Example usage:

long long fileSize = 14378165;
NSString *displayFileSize = [NSByteCountFormatter stringFromByteCount:fileSize
                                                           countStyle:NSByteCountFormatterCountStyleFile];
NSLog(@"Display file size: %@", displayFileSize);

fileSize = 50291;
displayFileSize = [NSByteCountFormatter stringFromByteCount:fileSize
                                                 countStyle:NSByteCountFormatterCountStyleFile];
NSLog(@"Display file size: %@", displayFileSize);

Log output:

Display file size: 14.4 MB
Display file size: 50 KB

The output will be formatted properly according to the device's regional settings.

Available since iOS 6.0 and OS X 10.8.

Michael Thiel
  • 2,434
  • 23
  • 21
0

Get the file size and then calculate whether it is in bytes or kb or mb

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];

NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];



Then conversion table

    1 byte = 8 bits

    1 KiB = 1,024 bytes

    1 MiB = 1024  kb

    1 GiB = 1024  mb

check this link

jonsca
  • 10,218
  • 26
  • 54
  • 62
zahreelay
  • 1,742
  • 12
  • 18
  • OP definitely understands how conversion works but is looking for a library/code snippet that does the necessary conversion. – Joe May 15 '12 at 20:01
  • if the file size can be calculated, you just have to divide by 1024 – zahreelay May 15 '12 at 20:15
0

Here's some code I found lying around. Not terribly efficient, and probably better attached to a category of NSNumberFormatter than NSNumber, and so on, but it seems to work

@interface NSNumber (FormatKibi)
- (NSString *)formatKibi;
@end

@implementation NSNumber (FormatKibi)
- (NSString *)formatKibi {
  double value = [self doubleValue];
  static const char suffixes[] = { 0, 'k', 'm', 'g', 't' }; 
  int suffix = 0;
  if (value <= 10000)
    return [[NSString stringWithFormat:@"%5f", value] 
         substringToIndex:5];
  while (value > 9999) {
    value /= 1024.0;
    ++suffix;
    if (suffix >= sizeof(suffixes)) return @"!!!!!";
  }
  return [[[NSString stringWithFormat:@"%4f", value] 
            substringToIndex:4]
           stringByAppendingFormat:@"%c", suffixes[suffix]];
}
@end

I tested it with this:

int main(int argc, char *argv[]) {
  for (int i = 1; i != argc; ++i) {
    NSNumber *n = [NSNumber numberWithInteger:
                              [[NSString stringWithUTF8String:argv[i]]
                                integerValue]];
    printf("%s ", [[n formatKibi] UTF8String]);
  }
  printf("\n");
  return 0;
}

Then:

$ ./sizeformat 1 12 123 1234 12345 123456 1234567 12345678 1234567890 123456789012 12345678901234 1234567890123456 123456789012345678
1.000 12.00 123.0 1234. 12.0k 120.k 1205k 11.7m 1177m 114.g 11.2t 1122t !!!!!
abarnert
  • 354,177
  • 51
  • 601
  • 671