Re: NSNumberFormatter not working for me ?
Re: NSNumberFormatter not working for me ?
- Subject: Re: NSNumberFormatter not working for me ?
- From: Bill Hernandez <email@hidden>
- Date: Wed, 14 Apr 2010 20:38:17 -0500
On Apr 14, 2010, at 5:25 PM, Greg Guerin wrote:
> Your code formats strings (more specifically, characters in strings). It does not format numbers, as such.
This is the work-around that I did because I could not make do with NSNumberFormatter.
> By "number" I mean a binary numeric value (floating-point or integer), or possibly NSNumber or NSDecimalNumber.
I've been programming the mac since 1987 pretty much full-time. so I promise you, I am not confused at all about what a number is, and isn't...
> All your "number" parameters are actually of the NSString* type, not of a numeric type. The fact that the string contains digits is incidental. In a sense, converting a numeric value to NSString* is already a "formatting" operation, or at least a conversion operation.
I think you missed the earlier messages. You are probably looking at the converter that I wrote as a work-around, which is basically a numeric string formatter.
> Your code would work just as well if you passed it an alphabetic string, or one containing punctuation marks.
>
> strippedNumber = @"SueMeTomorrow"
> format = @"Social Security : ###-##-###"
> result = @"Social Security : Sue-Me-Tom"
>
> I'm not saying the digit-string isn't relevant to what you're doing, only that what you seem to think of as a number is, in fact, a string that happens to contain a series of digit characters. I think that was a point an earlier reply was trying to make: NSNumberFormatter is for numeric values (NSNumber, in particular), not string values that happen to contain digits.
I think you missed the previous message, where someone else made your same incorrect assumption, only to have that cleared up by Jens Alfke. Jens picked up that I was actually using an NSNumber with the NSNumberFormatter. Please look at the comment from Jens Alfke , and the three lines of code below, they should clear that up for you.
> only that what you seem to think of as a number is
I promise you I know what a number is...
This got answered a while back...
--------------------------------------------------------
On Apr 13, 2010, at 4:55 PM, Jens Alfke wrote:
> On Apr 13, 2010, at 2:49 PM, Keary Suska wrote:
>
>> You are asking the NSNumberFormatters to format a string, which it does not do (hence the class name).
>
> No he isn't. Viz:
>
> NSInteger theInt = [aNumberString intValue];
> NSNumber *theNum = [NSNumber numberWithInt:theInt];
> NSString *theString = (NSString *)[numberFormatter stringFromNumber:theNum];
>
> —Jens
--------------------------------------------------------
Here's the original piece of code that you must have missed, please read the code carefully.
I added two comments in the code in case you missed the NSNumber line.
// <--- NSUInteger (look for these below)
// <--- NSNumber (look for these below)
// +---------+---------+---------+---------+---------+---------+---------+---------+
- (void)awakeFromNib
{
// I have already run a routine to strip all non digits by this time
NSString *strippedNumber = @"1234567890";
NSString *phoneNumber;
NSString *format;
format = @"(###) ###-####";
phoneNumber = [BHUtility bhFormatNumberString:strippedNumber withFormat:format];
}
// +---------+---------+---------+---------+---------+---------+---------+---------+
// +---------+---------+---------+---------+---------+---------+---------+---------+
+ (NSString *)unusable_bhFormatNumberString:(NSString *)aNumberString
withFormat:(NSString *)aFormat
{
// THIS METHOD DOES NOT WORK, WITH OR WITHOUT THE NEXT LINE
// YIELDS INCORRECT RESULTS : @"(1234567890) -"
[NSNumberFormatter setDefaultFormatterBehavior:NSNumberFormatterBehavior10_0];
NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
// NSFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[numberFormatter setFormat:aFormat]; // specify just positive values format
NSUInteger theInt = [aNumberString intValue]; // <--- NSUInteger
NSNumber *theNum = [NSNumber numberWithInt:theInt]; // <--- NSNumber
NSString *theString = (NSString *)[numberFormatter stringFromNumber:theNum];
NSLog(@"[4625] theString = %@", theString);
return theString;
// +---------+---------+---------+---------+---------+---------+---------+---------+
}
// +---------+---------+---------+---------+---------+---------+---------+---------+
_______________________________________________
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