-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.m
48 lines (41 loc) · 984 Bytes
/
Util.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
//
// Util.m
// iResist
//
//
#import "Util.h"
NSString *prettyPrintOhms (double ohms, int precision)
{
NSMutableString *prettyStr = [[NSMutableString alloc] init];
int exp = (int)log10(ohms);
int curPow = 0;
NSString *magStr = @"";
if (exp >= 9) {
magStr = @"G";
curPow = 9;
} else if (exp >= 6) {
magStr = @"M";
curPow = 6;
} else if (exp >= 3) {
magStr = @"K";
curPow = 3;
}
double sigOhms = ohms / pow(10.0, (double)curPow);
int precisionNeeded = precision;
int ii;
for (ii = precision; ii > 0; ii--) {
if ((int)(sigOhms * pow(10.0, (double)ii)) % 10 == 0) {
precisionNeeded--;
} else {
break;
}
}
if (precisionNeeded == 0) {
[prettyStr appendFormat:@"%d", (int)sigOhms];
} else {
NSString *formatString = [NSString stringWithFormat:@"%%.\%df", precisionNeeded];
[prettyStr appendFormat:formatString, sigOhms];
}
[prettyStr appendFormat:@" %@Ω", magStr];
return [prettyStr autorelease];
}