Created
June 7, 2013 10:07
-
-
Save jrturton/5728311 to your computer and use it in GitHub Desktop.
Category for adding currency indicators to NSStrings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface NSString (CurrencyIndicators) | |
-(NSString*)stringByAddingCurrencyIndicator:(NSString*)indicatorCode; | |
@end | |
@implementation NSString (CurrencyIndicators) | |
-(NSString*)stringByAddingCurrencyIndicator:(NSString*)indicatorCode | |
{ | |
NSString *formatString = nil; | |
NSString *prefix = nil; | |
NSString *number = nil; | |
// Remove any sign from the front of the string | |
if ([self hasPrefix:@"+"] || [self hasPrefix:@"-"]) | |
{ | |
prefix = [self substringToIndex:1]; | |
number = [self substringFromIndex:1]; | |
} | |
else | |
{ | |
prefix = @""; | |
number = self; | |
} | |
if ([indicatorCode isEqualToString:@"GBX"]) | |
formatString = @"%@p"; | |
else if ([indicatorCode isEqualToString:@"GBP"]) | |
formatString = @"£%@"; | |
else if ([indicatorCode isEqualToString:@"USD"]) | |
formatString = @"$%@"; | |
else if ([indicatorCode isEqualToString:@"EUR"]) | |
formatString = @"€%@"; | |
else if ([indicatorCode isEqualToString:@"n/a"]) | |
return self; | |
else | |
{ | |
NSAssert1(NO,@"Unhandled currency indicator %@",indicatorCode); | |
return [NSString stringWithFormat:@"%@ %@",self,indicatorCode]; | |
} | |
return [prefix stringByAppendingString:[NSString stringWithFormat:formatString,number]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment