3

我正在使用NSNumberFormatter将我的数字格式化为字符串。我有一个具有希伯来语(以色列)区域格式(设置->常规->国际->区域格式)的设备。例如,当我尝试格式化数字 100 时,我得到 100 美元。我的目标是删除货币符号前的空格并仅获得 100 美元

4

2 回答 2

5

我最终通过删除空格来更改 positiveSuffix 和negativeSuffix 属性

因为我的 NSNumberFormatter 在我的应用程序中是静态的,所以我在每次使用结束时将它们设置为 nil

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 回答