Question

I have an Array , if I print that in console it shows

Price Array=(
    "$100",
    "$300"
)

I need to add add objects at each index and show it in label. Please suggest any idea, in this case how it will show 400 with $ sign? I tried this in view did load

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);`

where in interface float itemPrize,tempVar,total


This is what i did

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);
Was it helpful?

Solution

To get rid of $ sign you should try: [[itemPrize componentsSeparatedByString:@"$"] objectAtIndex:1];. Then you should use stringWithFormat method to do the string formatting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top