-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataLenghtManager.m
executable file
·158 lines (131 loc) · 4.56 KB
/
DataLenghtManager.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// DataLenghtManager.m
// Device Inspector
//
// Created by Rogerio Tomio Hirooka on 12/22/11.
// Copyright (c) 2011 Lirum Labs T Sistemas. All rights reserved.
//
#import "DataLenghtManager.h"
@implementation DataLenghtManager
float const AUTO = 0.0f;
float const BYTES = 1.0f;
float const KB = 1024.0f;
float const MB = 1048576.0f;
float const GB = 1073741824.0f;
float const TB = 1099511627776.0f;
float const PB = 1125899906842624.0f;
float const Un = 1.0f;
float const KUn = 1000.0f;
float const MUn = 1000000.0f;
float const GUn = 1000000000.0f;
float const TUn = 1000000000000.0f;
float const PUn = 1000000000000000.0f;
static NSNumberFormatter *formatter = nil;
+(NSNumberFormatter *) getFormatter
{
if (!formatter)
{
formatter = [[NSNumberFormatter alloc] init];
[formatter setGroupingSize:3];
[formatter setUsesGroupingSeparator:YES];
}
return formatter;
}
+(NSString *)bytesToMultipleString: (float)input
targetUnit: (float)unit
numberOfDecimals: (int)decimals
{
return [self bytesToMultipleString:input
targetUnit:unit
numberOfDecimals:decimals
breakUnit:FALSE];
}
+(NSString *)bytesToMultipleString: (float)input
targetUnit: (float)unit
numberOfDecimals: (int)decimals
breakUnit: (BOOL)breakUnit
{
NSString *retVal =
[self bytesToMultipleString:input
targetUnit:unit
numberOfDecimals:decimals
breakUnit:breakUnit
ommitUnit:FALSE];
return retVal;
}
+(NSString *)bytesToMultipleString: (float)input
targetUnit: (float)unit
numberOfDecimals: (int)decimals
breakUnit: (BOOL)breakUnit
ommitUnit: (BOOL)ommitUnit
{
float preResult = 0;
if (unit == AUTO)
{
if (input < KB) unit = BYTES;
else if (input < MB) unit = KB;
else if (input < GB) unit = MB;
else if (input < TB) unit = GB;
else if (input < PB) unit = TB;
else unit = PB;
}
NSString *stringUnit;
if (unit == BYTES) stringUnit = @"bytes";
else if (unit == KB) stringUnit = @"KB";
else if (unit == MB) stringUnit = @"MB";
else if (unit == GB) stringUnit = @"GB";
else if (unit == TB) stringUnit = @"TB";
else if (unit == PB) stringUnit = @"PB";
preResult = input / unit;
if (unit == BYTES) decimals = 0;
NSNumberFormatter *form = [self getFormatter];
[form setMinimumFractionDigits:decimals];
[form setMaximumFractionDigits:decimals];
NSString *format1 = @"%@ %@";
NSString *format2 = @"%@\n%@";
if (ommitUnit) stringUnit = @"";
return [NSString stringWithFormat: breakUnit ? format2 : format1,
[form stringFromNumber:[NSNumber numberWithFloat:preResult]], stringUnit];
}
+(NSString *) multipleString: (float)input
targetUnit: (float)unit
numberOfDecimals: (int) decimals
unitSymbol: (NSString *)unitSymbol
{
float preResult = 0;
if (unit == AUTO)
{
if (input < KUn) unit = Un;
else if (input < MUn) unit = KUn;
else if (input < GUn) unit = MUn;
else if (input < TUn) unit = GUn;
else if (input < PUn) unit = TUn;
else unit = PUn;
}
NSString *stringUnit;
if (unit == Un) stringUnit = @"";
else if (unit == KUn) stringUnit = @"K";
else if (unit == MUn) stringUnit = @"M";
else if (unit == GUn) stringUnit = @"G";
else if (unit == TUn) stringUnit = @"T";
else if (unit == PUn) stringUnit = @"P";
preResult = input / unit;
if (unit == KUn) decimals = 0;
NSNumberFormatter *form = [self getFormatter];
[form setMinimumFractionDigits:decimals];
[form setMaximumFractionDigits:decimals];
NSString *format1 = @"%@ %@%@";
return [NSString stringWithFormat: format1,
[form stringFromNumber:[NSNumber numberWithFloat:preResult]],
stringUnit,
unitSymbol];
}
+(NSString *)decimalString: (float)input numberOfDecimals:(int)decimals
{
NSNumberFormatter *form = [self getFormatter];
[form setMinimumFractionDigits:decimals];
[form setMaximumFractionDigits:decimals];
return [NSString stringWithFormat:@"%@",
[form stringFromNumber:[NSNumber numberWithFloat:input]]];
}
@end