3

NSNumberFormatter数値を文字列にフォーマットするために使用しています。ヘブライ語 (イスラエル) 地域形式のデバイスを持っています (設定 -> 一般 -> 国際 -> 地域形式)。たとえば、数値 100 をフォーマットしようとすると、100 $ が得られます。私の目標は、通貨記号の前のスペースを削除して、ちょうど 100$ を取得することです

4

2 に答える 2

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;
于 2013-03-12T09:36:06.033 に答える
0

NSNumberFormatterを実行する前に、文字列からすべてのスペースを削除するのはどうですか?(この質問に答えたように)

NSString *stringWithoutSpaces = [myString 
    stringByReplacingOccurrencesOfString:@" " withString:@""];
于 2013-03-11T14:57:25.373 に答える