Ios 如何将货币字符串转换为数字
我有以下字符串: R$1.234.567,89 我需要它看起来像:1.234.567.89 我该怎么做 这就是我所尝试的: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:@"."]
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