我有一个数组,如果我在控制台中打印它会显示
Price Array=(
"$100",
"$300"
)
我需要在每个索引处添加添加对象并在标签中显示它。请提出任何想法,在这种情况下它将如何显示带有$符号的400? 我在视图中尝试加载
for (int j=0; j<[cartArray2 count]; j++)
{
itemPrize =[prizelabel.text floatValue];
tempVar=itemPrize;
total=total+tempVar;
NSString *strTotalAmt = [NSString stringWithFormat:@"%f",total];
prizelabel.text=strTotalAmt;
}
NSLog( @"Toatl= %f",total);`
在接口float itemPrize,tempVar,total
中这就是我做的
for (int j=0; j<[cartArray2 count]; j++)
{
NSMutableString *cleanedText = [NSMutableString stringWithCapacity:0];
NSString *newRecord = [[cartArray2 objectAtIndex:j] stringByReplacingOccurrencesOfString:@"$" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, [[cartArray2 objectAtIndex:j] length])];
[cleanedText appendFormat:@"%@\n", newRecord];
NSLog(@"Cleaned=%@", cleanedText);
itemPrize =[cleanedText intValue];
tempVar=itemPrize;
total=total+tempVar;
NSString *strTotalAmt = [NSString stringWithFormat:@"%d",total];
prizelabel.text=strTotalAmt;
}
NSLog( @"Toatl= %d",total);
答案 0 :(得分:1)
要摆脱$ sign,你应该尝试:[[itemPrize componentsSeparatedByString:@"$"] objectAtIndex:1];
。然后,您应该使用stringWithFormat
方法进行字符串格式化。