Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将货币字符串转换为数字_Ios_Currency_Nsnumberformatter - Fatal编程技术网

Ios 如何将货币字符串转换为数字

Ios 如何将货币字符串转换为数字,ios,currency,nsnumberformatter,Ios,Currency,Nsnumberformatter,我有以下字符串: R$1.234.567,89 我需要它看起来像:1.234.567.89 我该怎么做 这就是我所尝试的: NSString* cleanedString = [myString stringByReplacingOccurrencesOfString:@"." withString:@""]; cleanedString = [[cleanedString stringByReplacingOccurrencesOfString:@"," withString:@"."]

我有以下字符串:

R$1.234.567,89

我需要它看起来像:1.234.567.89

我该怎么做

这就是我所尝试的:

NSString* cleanedString = [myString stringByReplacingOccurrencesOfString:@"." withString:@""];
cleanedString = [[cleanedString stringByReplacingOccurrencesOfString:@"," withString:@"."]
                                     stringByTrimmingCharactersInSet: [NSCharacterSet symbolCharacterSet]];

这是可行的,但我认为一定有更好的办法。建议?

如果只想从字符串中删除前两个字符,可以这样做

NSString *cleanedString = [myString substringFromIndex:2];

如果您的数字始终在$之后,但前面有更多字符,则可以将其设置为:

NSString* test = @"R$1.234.567,89";
NSString* test2 = @"TESTERR$1.234.567,89";
NSString* test3 = @"HEllo123344R$1.234.567,89";


NSLog(@"%@",[self makeCleanedText:test]);
NSLog(@"%@",[self makeCleanedText:test2]);
NSLog(@"%@",[self makeCleanedText:test3]);
方法是:

- (NSString*) makeCleanedText:(NSString*) text{

    int indexFrom = 0;

    for (NSInteger charIdx=0; charIdx<[text length]; charIdx++)
        if ( '$' == [text characterAtIndex:charIdx])
            indexFrom = charIdx + 1;

    text = [text stringByReplacingOccurrencesOfString:@"," withString:@"."];
    return [text substringFromIndex:indexFrom];
}
2013-10-20 22:35:39.726 test[40546:60b] 1.234.567.89
2013-10-20 22:35:39.728 test[40546:60b] 1.234.567.89
2013-10-20 22:35:39.731 test[40546:60b] 1.234.567.89