-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathX_NumberFormatter.m
62 lines (45 loc) · 1.14 KB
/
X_NumberFormatter.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
//
// X_NumberFormatter.m
// FluidApp
//
#import "X_NumberFormatter.h"
@implementation X_NumberFormatter
- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error
{
int x;
int l = [partialString length];
BOOL foundDecimal = NO;
int curPostDecimal = 0;
for (x=0; x<l; x++)
{
unichar curChar = [partialString characterAtIndex:x];
if (!(curChar >= '0' && curChar <= '9') && curChar != '.' &&
!(x ==0 && (curChar == '+' || curChar == '-')))
return NO;
if (curChar >= '0' && curChar <= '9')
{
curPostDecimal++;
if (curPostDecimal > 12)
return NO;
}
if (curChar == '.')
{
if (foundDecimal)
return NO;
foundDecimal = YES;
}
}
return YES;
}
- (BOOL)getObjectValue:(id *)anObject forString:(NSString *)string errorDescription:(NSString **)error
{
*anObject = [NSNumber numberWithDouble:[string doubleValue]];
return YES;
}
- (NSString *)stringForObjectValue:(id)anObject
{
if (![anObject isKindOfClass:[NSNumber class]])
return nil;
return [NSString stringWithFormat:@"%1.12g", [anObject doubleValue]];
}
@end