forked from sparkle-project/Sparkle
-
Notifications
You must be signed in to change notification settings - Fork 2
/
NSNumber+Units.m
28 lines (21 loc) · 858 Bytes
/
NSNumber+Units.m
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
27
28
//
// NSNumber+Units.m
// Sparkle
//
// Created by Jonas Witt on 5/18/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "NSNumber+Units.h"
#import "Sparkle.h"
@implementation NSNumber (Units)
+ (NSString *)humanReadableSizeFromDouble:(double)value
{
if (value < 1024)
return [NSString stringWithFormat:@"%.0lf %@", value, SULocalizedString(@"B", @"the unit for bytes")];
if (value < 1024 * 1024)
return [NSString stringWithFormat:@"%.0lf %@", value / 1024.0, SULocalizedString(@"kB", @"the unit for kilobytes")];
if (value < 1024 * 1024 * 1024)
return [NSString stringWithFormat:@"%.1lf %@", value / 1024.0 / 1024.0, SULocalizedString(@"MB", @"the unit for megabytes")];
return [NSString stringWithFormat:@"%.2lf %@", value / 1024.0 / 1024.0 / 1024.0, SULocalizedString(@"GB", @"the unit for gigabytes")];
}
@end