NSNumberFormatter,如何删除货币符号中的空格 [英] NSNumberFormatter, how to remove blank spaces in currency symbol

查看:483
本文介绍了NSNumberFormatter,如何删除货币符号中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些值,我需要格式化为货币字符串。看起来当使用NSNumberFormatter格式化一个金额时,结果货币字符串将包含一个或多个空格。



例如使用下面的代码格式 @1000转换为欧洲货币格式将导致返回 @1,000,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返回格式化的字符串没有blanc空格? ( 1.000,00€

解决方案

刚刚遇到了同样的问题。 NSNumberFormatter使用的空格字符不是您在替换方法中提供的标准空格。



这是一种特殊字符。你可以使用NSLog检索它,然后从你的调试窗口复制那个神秘的空间并粘贴到你的替换方法

  formattedPrice = [ formattedPrice stringByReplacingOccurrencesOfString:@withString:@]; 

复制和粘贴我的行也应该做(不知道浏览器用它。 ..)


I'm having some values that I need to format into a currency string. It seems that when using NSNumberFormatter to format an amount, the resulting currency string will contain one or more blank spaces.

For instance using the following piece of code to format @"1000" into the European currency format will result in returning @"1,000,00 €". Note the blank space before the currency symbol.

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 € 

I first thought to solve this easily by just filtering the spaces out of the string but for some reason this does not work, the following piece of code is not doing what I was expecting:

[result replaceOccurrencesOfString:@" " withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [result length])];

Is there any way using NSNumberFormatter to return the formatted string without blanc spaces in it ? (1.000,00€)

解决方案

It's an old question but I just ran into the same problem. The space character which NSNumberFormatter is using is not the standard space you provided in your replace method.

It is some kind of special character. You can retrieve it by using NSLog then copy that mysterious space from your debug window and paste it into your replace method

formattedPrice = [formattedPrice stringByReplacingOccurrencesOfString:@" " withString:@""];

Copy and paste my line should also do (don't know what the browser does with it though...)

这篇关于NSNumberFormatter,如何删除货币符号中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆