summaryrefslogtreecommitdiff
path: root/UICaboodle/UCString.h
blob: 77ee6a1b3060884e2055abe82b95bc4108adcda2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef UICABOODLE_UCSTRING_H
#define UICABOODLE_UCSTRING_H

#import <Foundation/NSString.h>

@interface NSString (UICaboodle)
+ (NSString *) stringWithDataSize:(double)size;
@end

@implementation NSString (UICaboodle)

+ (NSString *) stringWithDataSize:(double)size {
    unsigned power = 0;
    while (size > 1024) {
        size /= 1024;
        ++power;
    }

    static const char *powers_[] = {"B", "KiB", "MiB", "GiB"};

    return [NSString stringWithFormat:@"%.1f%s", size, powers_[power]];
}

@end

#endif/*UICABOODLE_UCSTRING_H*/