Re: NSNumberFormatter not working for me ? [SOLVED]
Re: NSNumberFormatter not working for me ? [SOLVED]
- Subject: Re: NSNumberFormatter not working for me ? [SOLVED]
- From: Bill Hernandez <email@hidden>
- Date: Tue, 13 Apr 2010 22:46:35 -0500
This is a great forum, I can't believe all the people that pitched in to help...
Thanks everybody for all the time and help...
This is actually a really nice number formatting method that will go in my BHUtility.m You can format any way you want....
Notice that the first three results are 10 digits, after I had stripped out all non-digits, and the fourth example contains 14 digits after all the junk chars are stripped out. I wrote a couple of methods that strip out leading, trailing spaces, converts multiple spaces to single spaces.
The formatters also do capitalization after cleanup, convert middle names to capitalized middle initials with a period on the end, convert state names to short state initials, etc.
In this case the formatter strips out everything except digits
If anybody is interested in these formatter methods, I can post them.
Thanks again,
Bill Hernandez
Plano, Texas
RESULTS:
Loading program into debugger…
Program loaded.
run
[Switching to process 6148]
Running…
2010-04-13 22:29:15.777 Formatter[6261:a0f] [4626] returnString = (123) 456-7890
2010-04-13 22:29:15.779 Formatter[6261:a0f] [4626] returnString = 123.456.7890
2010-04-13 22:29:15.779 Formatter[6261:a0f] [4626] returnString = <[123.456.7890]>
2010-04-13 22:29:15.780 Formatter[6261:a0f] [4626] returnString = (123) 456-7890 [extension 1234]
kill
quit
The Debugger has exited with status 0.
// +---------+---------+---------+---------+---------+---------+---------+---------+
- (void)awakeFromNib
{
// I have already run a routine to strip all non digits by this time
NSString *strippedNumber = @"1234567890";
NSString *phoneNumber;
BOOL useFormatter = YES;
if(useFormatter)
{
phoneNumber = [BHUtility bhFormatNumberString:strippedNumber withFormat:@"(###) ###-####"];
phoneNumber = [BHUtility bhFormatNumberString:strippedNumber withFormat:@"###.###.####"];
phoneNumber = [BHUtility bhFormatNumberString:strippedNumber withFormat:@"<[###.###.####]>"];
strippedNumber = @"12345678901234"; // notice this is 14 digits long now
if([strippedNumber length] == 14)
{
phoneNumber = [BHUtility bhFormatNumberString:strippedNumber withFormat:@"(###) ###-#### [extension ####]"];
}
else
{
phoneNumber = strippedNumber;
}
}
else
{
// THIS IS NOT VERY FLEXIBLE
if([strippedNumber length] == 10)
{
NSString *areacode = [strippedNumber substringWithRange:NSMakeRange(0, 3)];
NSString *exchange = [strippedNumber substringWithRange:NSMakeRange(3, 3)];
NSString *number = [strippedNumber substringWithRange:NSMakeRange(6, 4)];
phoneNumber = [NSString stringWithFormat:@"(%@) %@-%@", areacode, exchange, number];
}
else
{
phoneNumber = strippedNumber;
}
}
NSLog(@"[4626] phoneNumber = %@", phoneNumber);
}
// +---------+---------+---------+---------+---------+---------+---------+---------+
// +---------+---------+---------+---------+---------+---------+---------+---------+
+ (NSString *)bhFormatNumberString:(NSString *)aNumberString withFormat:(NSString *)aFormat
{
NSString *myFormatString = [[NSString stringWithString:aFormat] copy];
NSUInteger myFormatStringLength = [myFormatString length];
NSMutableString *returnString = [[[NSMutableString alloc] initWithCapacity:myFormatStringLength] autorelease];
NSUInteger i = 0; // i represents the myFormatString character position/counter through the loop
NSUInteger aNumberStringPosition = 0;
unichar myUniChar;
for ( i = 0; i < myFormatStringLength; i ++ )
{
myUniChar = [myFormatString characterAtIndex:i];
// NSLog (@"[4525] i = %i: myUniChar = %C\n", i, myUniChar);
if (myUniChar == '#')
{
// aNumberStringPosition += 1;
myUniChar = [aNumberString characterAtIndex:aNumberStringPosition++];
}
[returnString appendFormat:@"%C",myUniChar];
}
NSLog(@"[4626] returnString = %@", returnString);
return returnString;
}
// +---------+---------+---------+---------+---------+---------+---------+---------+
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden