NSNumberFormatter not working for me ?
NSNumberFormatter not working for me ?
- Subject: NSNumberFormatter not working for me ?
- From: Bill Hernandez <email@hidden>
- Date: Tue, 13 Apr 2010 16:15:42 -0500
I am calling a method that I pass a phone number string "1234567890" and should return "(123) 456-7890" but sure enough, that is not what is happening...
Any Ideas ?
Bil Hernandez
Plano, Texas
I created a simple demo below :
$ clang_gen
---> shows : ** BUILD SUCCEEDED **
Build and Analyze shows :
---> Build Succeeded
---> No Issues
NSLog shows :
Switching to process 2491
Running…
(gdb) continue
Current language: auto; currently objective-c
2010-04-13 15:04:01.829 Formatter2491:a0f 4625 theString = 1234567890
2010-04-13 15:04:01.832 Formatter2491:a0f 4626 phoneNumber = 1234567890
kill
quit
The Debugger has exited with status 0.
// +---------+---------+---------+---------+---------+---------+---------+---------+
// BHFormatter.h
// +---------+---------+---------+---------+---------+---------+---------+---------+
#import <Cocoa/Cocoa.h>
@interface BHFormatter : NSObject
{
}
@end
// +---------+---------+---------+---------+---------+---------+---------+---------+
// BHFormatter.m
// +---------+---------+---------+---------+---------+---------+---------+---------+
#import "BHFormatter.h"
#import "BHUtility.h"
@implementation BHFormatter
// +---------+---------+---------+---------+---------+---------+---------+---------+
- (void)awakeFromNib
{
// I have already run a routine to strip all non digits by this time
NSString *strippedNumber = @"1234567890";
NSString *phoneNumber;
phoneNumber = [BHUtility bhFormatNumberString:strippedNumber withFormat:@"(###) ###-####"];
NSLog(@"[4626] phoneNumber = %@", phoneNumber);
}
// +---------+---------+---------+---------+---------+---------+---------+---------+
@end
// +---------+---------+---------+---------+---------+---------+---------+---------+
// BHUtility.h
// +---------+---------+---------+---------+---------+---------+---------+---------+
#import <Cocoa/Cocoa.h>
@interface BHUtility : NSObject
{
}
+ (NSString *)bhFormatNumberString:(NSString *)aNumberString withFormat:(NSString *)aFormat;
@end
// +---------+---------+---------+---------+---------+---------+---------+---------+
// BHUtility.m
// +---------+---------+---------+---------+---------+---------+---------+---------+
#import "BHUtility.h"
@implementation BHUtility
// +---------+---------+---------+---------+---------+---------+---------+---------+
+ (NSString *)bhFormatNumberString:(NSString *)aNumberString withFormat:(NSString *)aFormat
{
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
// NSFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setFormat:aFormat]; // specify just positive values format
NSInteger theInt = [aNumberString intValue];
NSNumber *theNum = [NSNumber numberWithInt:theInt];
NSString *theString = (NSString *)[numberFormatter stringFromNumber:theNum];
NSLog(@"[4625] theString = %@", theString);
return theString;
// +---------+---------+---------+---------+---------+---------+---------+---------+
}
@end
_______________________________________________
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