3

I am using NSNumberFormatter to format my numbers to strings. I have a device with Hebrew (israel) region format (settings->General->International->Region Format). When I try to format the number 100 for instance I get 100 $. My goal is to remove the space before the currency sign and get just 100$

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
user1746700
  • 117
  • 11

2 Answers2

5

I ended up changing positiveSuffix and negativeSuffix properties by removing the spaces from them

because my NSNumberFormatter is static in my application I set them to nil at the end of each use

static NSNumberFormatter *currencyFormatter;
if (!currencyFormatter) {
     currencyFormatter = [[NSNumberFormatter alloc] init];
     [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
     [currencyFormatter setNegativeFormat:@""];
}

// remove spaces at the suffix
currencyFormatter.positiveSuffix = [currencyFormatter.positiveSuffix stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

currencyFormatter.negativeSuffix = [currencyFormatter.negativeSuffix stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];


// get the return number
NSString* retNum = [currencyFormatter stringFromNumber:val];

// this code is for the next time using currencyFormatter
currencyFormatter.positiveSuffix = nil;
currencyFormatter.negativeSuffix = nil;

return retNum;
user1746700
  • 117
  • 11
0

How about just removing all spaces from the string before running it through NSNumberFormatter? (as answered in this question)

NSString *stringWithoutSpaces = [myString 
    stringByReplacingOccurrencesOfString:@" " withString:@""];
Community
  • 1
  • 1
Fls'Zen
  • 4,594
  • 1
  • 29
  • 37