-3

アレイがあります。コンソールでそれを印刷すると、次のように表示されます。

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

インターフェイスfloatitemPrize、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);
4

1 に答える 1

1

$ 記号を取り除くには、以下を試してください: [[itemPrize componentsSeparatedByString:@"$"] objectAtIndex:1];. 次に、stringWithFormatメソッドを使用して文字列の書式設定を行う必要があります。

于 2012-06-19T09:40:41.887 に答える