-3

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);
casperOne
  • 73,706
  • 19
  • 184
  • 253
Ajit Satarkar
  • 717
  • 6
  • 20
  • @Ajit Satarkar can we see some code? – self Jun 19 '12 at 09:09
  • @Thilo I tried this in View did load NSLog(@"Price Array=%@",cartArray2); 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; } where float itemPrize,tempVar, total is declared globally. – Ajit Satarkar Jun 19 '12 at 09:17
  • You need to get rid of the "$" before calling floatValue. And add it later when printing the result. – Thilo Jun 19 '12 at 09:26
  • @Thilo Actually that $ sign default-ally comes because i got this items by parsing JSON – Ajit Satarkar Jun 19 '12 at 09:30
  • I know, but you can use string manipulation functions (substring comes to mind) to cut it out. – Thilo Jun 19 '12 at 09:32
  • @Thilo Thank you it works properly now.. – Ajit Satarkar Jun 19 '12 at 11:09

1 Answers1

1

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

Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39