Reputation: 1180
this is my string i want to place the variable float point in this string:
i want to pass the value in data type of float.
such as..%.1f %.2f i want to pass the 1 and 2 value dynamically in iPhone sdk i use this code :-----
NSString *string=[NSString stringWithFormat:@"%.%if",[[arrayvalue objectAtIndex:0] intValue],[textField2.text floatValue]];
but it print only:-- .if
Upvotes: 0
Views: 797
Reputation: 814
NSMutableString can add another string or format.
// array must have first object pass 1st parameter
NSMutableString *mutableString = [[NSString alloc] init];
[str appendFormat:"%d", [array objectAtIndex:0].intValue];
// array have second object then pass 2nd parameter
if (2 <= array.count)
[mutableString appendFormat:"%f", [array objectAtIndex:0].floatValue]];
NSLog("%@", mutableString);
Upvotes: 0
Reputation: 3663
You forgot to add Escape character for '%' this sign. Try below code it will work.
int i=2;
NSString *output= [NSString stringWithFormat:@"%%.%if",i];
if you want to display two values then you can try below.
int i=2;
float x=3.5;
NSString *output= [NSString stringWithFormat:@"%%.%if %%.%ff",i,x];
Please modify as you need. This is just for your idea how to use it.
Upvotes: 0
Reputation: 69489
You can't nest format string, you could do it like this:
int value = [[arrayvalue objectAtIndex:0] intValue];
NSString *string = [@"%." stringByAppendingFormat:@"%if", value];
string = [NSString stringWithFormat:string, [textField2.text floatValue]];
This is code is totally untested.
Upvotes: 1