tags:

views:

36

answers:

1

I have an Objective-C method that formats a double to a currency NSString:

- (NSString*) formatCurrencyValue:(double)value
{
    NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehaviorDefault];
    [numberFormatter setCurrencySymbol:@"$"];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    NSNumber *c = [NSNumber numberWithFloat:value];

    NSString* stringValue = [numberFormatter stringFromNumber:c];

    NSLog(@"currencyFormatter=%@",stringValue);

    return stringValue;
}

The NSLog statement correctly prints the formatted double. However, when I call this method and assign it another NSString object, its value is empty.

For example:

NSString* key = [self formatCurrencyValue:menuItem.Price];

NSLog(@"Key value=", key);

The output from the NSLog statement there is:

Key value=

What's going on???

+1  A: 

In the line NSLog(@"Key value=", key);, you're missing the %@ placeholder that tells Objective-C where to print the argument.

It should be NSLog(@"Key value=%@", key);.

Mark Rushakoff
Curse the @%! Thanks!
Wayne Hartman
@Wayne: You're welcome. You would think that the compiler would warn you when you passed in too many arguments vs. format specifiers, but I remember reading somewhere that there's a specific reason this isn't done in Objective-C...
Mark Rushakoff