Iphone NSNumberFormatter,如何删除货币符号中的空格
我需要将一些值格式化为货币字符串。似乎在使用NSNumberFormatter格式化金额时,生成的货币字符串将包含一个或多个空格 例如,使用以下代码将Iphone NSNumberFormatter,如何删除货币符号中的空格,iphone,objective-c,cocoa,cocoa-touch,Iphone,Objective C,Cocoa,Cocoa Touch,我需要将一些值格式化为货币字符串。似乎在使用NSNumberFormatter格式化金额时,生成的货币字符串将包含一个或多个空格 例如,使用以下代码将@“1000”格式化为欧洲货币格式将导致返回@“1000,00欧元”。注意货币符号前的空白空间。 NSNumberFormatter *tempNumberFormatter = [[NSNumberFormatter alloc] init]; [tempNumberFormatter setFormatterBehavior:NSNumber
@“1000”
格式化为欧洲货币格式将导致返回@“1000,00欧元”
。注意货币符号前的空白空间。
NSNumberFormatter *tempNumberFormatter = [[NSNumberFormatter alloc] init];
[tempNumberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[tempNumberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[tempNumberFormatter setLocale:[NSLocale currentLocale]];
[tempNumberFormatter setGeneratesDecimalNumbers:YES];
[tempNumberFormatter setMaximumFractionDigits:2];
[tempNumberFormatter setMinimumFractionDigits:2];
[tempNumberFormatter setAlwaysShowsDecimalSeparator:YES];
NSString *value = @"1000";
NSNumber *number = [NSNumber numberWithFloat:[value doubleValue]];
NSString *result = [tempNumberFormatter stringFromNumber:number];
[tempNumberFormatter release];
// result = 1.000,00 €
我首先想通过过滤字符串中的空格来轻松解决这个问题,但由于某些原因,这不起作用,下面的代码没有达到我的预期:
[result replaceOccurrencesOfString:@" " withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [result length])];
是否有任何方法可以使用NSNumberFormatter返回格式化字符串,但其中不包含空格?(
1.000,00€
)您确定空间不适合您的区域设置吗?您的示例代码给了我“€1000.00”,这是我所在地区(美国)的正确格式。这是一个老问题,但我遇到了同样的问题。NSNumberFormatter使用的空格字符不是替换方法中提供的标准空格
这是一种特殊的性格。您可以使用NSLog检索它,然后从调试窗口复制神秘的空间,并将其粘贴到replace方法中
formattedPrice = [formattedPrice stringByReplacingOccurrencesOfString:@" " withString:@""];
复制并粘贴我的行也应该这样做(但不知道浏览器对它做了什么…为什么要创建一个字符串,将其解释为一个
双精度值,然后将其传递到需要浮点值的位置?为什么不使用floatValue
或numberwhithdouble:
?更好的是,为什么不像在[NSNumber numberWithDouble:1000.0]
中那样只传递一个浮点值
或double
文本呢?你为什么要毫无意识地寻找一个太空箱?谢谢你,这真的让我很难受。这是一个不间断的空格,Unicode 00A0: