Method on the class NSString in Apple's Cocoa framework; takes a printf-style format string and a variable number of other arguments to create an instance of NSString.
Questions tagged [stringwithformat]
125 questions
38
votes
2 answers
NSString stringWithFormat adding a percent
How can I add a percent to my stringWithFormat function? For example, I'd like to have the following:
float someFloat = 40.233f;
NSString *str = [NSString stringWithFormat:@"%.02f%",someFloat];
This should cause the string to be:
40.23%
But it is…

CodeGuy
- 28,427
- 76
- 200
- 317
23
votes
3 answers
How to prepend url with http:// using stringWithFormat in Swift
I'm building a custom browser using UIWebView.
Use case: User enters "www.abc.com" into the address bar. Error below thrown:
Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0x19860770…

kmiklas
- 13,085
- 22
- 67
- 103
21
votes
2 answers
comma separated thousand NSString stringWithFormat
Is it possible to display 1000.99 as 1,000.99 using
[NSString stringWithFormat:@"£%0.2f", 1000.99]
Please guide me if I am not on the right track to achieve this?

Leo
- 1,547
- 3
- 24
- 40
17
votes
1 answer
How does localizedStringWithFormat work?
Please note
I am not asking about NSLocalizedString() the macro. I am asking about the NSString class function + (instancetype)localizedStringWithFormat:(NSString *)format, ....
These are two separate things.
Question
I'm trying to use the NSString…

Fogmeister
- 76,236
- 42
- 207
- 306
16
votes
4 answers
NSString stringWithFormat
I don't know what I am missing here. I am trying to concatenate strings using [NSString stringWithFormat] function. This is what I am doing.
NSString *category = [row objectForKey:@"category"];
NSString *logonUser = [row…

Leo
- 1,547
- 3
- 24
- 40
12
votes
2 answers
In Swift, how to insert formatted Int value into String?
This seems like a super basic question, but I just can't seem to find the answer anywhere :-( I am able to do this in Objective C, but I am getting stuck in Swift.
What I need to do:
Take an Integer value
Format it into a localized string
Inject…

Erik van der Neut
- 2,245
- 2
- 22
- 21
11
votes
1 answer
NSString stringWithFormat Percent Sign
I searched and could not find this answer...
If I have this, how can I get it to display the final % symbol? It just omits it in my label.
[NSString stringWithFormat:@"Tries %i%", tries];

Jesse
- 891
- 1
- 13
- 22
9
votes
3 answers
dynamic string format with NSString stringWithFormat
I've been trying to make a dynamic string format so that through user options precision or padding could be somewhat user defined.
An example includes, the padding of leading zeros as in the 24hour time format. In normal time format, the hours can…

Madivad
- 2,999
- 7
- 33
- 60
8
votes
2 answers
objective-c stringvalue from double
I have the following code:
double d1 = 12.123456789012345;
NSString *test1 = [NSString stringWithFormat:@"%f", d1]; // string is: 12.123457
NSString *test1 = [NSString stringWithFormat:@"%g", d1]; // string is: 12.1235
How do I get a string value…

Shield
- 123
- 1
- 2
- 5
8
votes
1 answer
NSString stringWithFormat - trying to set a label to double digits for single digit numbers
I am trying to have a label display single-digit numbers as double-digit numbers (ie. 1 => 01) . I may be using the wrong method to set the value for the label but the thing is I'm not even sure about that after pouring over Apple's documentation…

b_d
- 777
- 2
- 7
- 18
7
votes
1 answer
How to append the string variables using stringWithFormat method in Objective-C
I want to append the string into single varilable using stringWithFormat.I knew it in using stringByAppendingString. Please help me to append using stringWithFormat for the below code.
NSString* curl = @"https://invoices?ticket=";
curl = [curl…

Madan Mohan
- 8,764
- 17
- 62
- 96
7
votes
4 answers
How to not show unnecessary zeros when given integers but still have float answers when needed
I have an app I'm developing and one of my features is giving answers in float or double values when needed and an integer when the answer is a whole number
so for example if the answer comes out to 8.52 the answer becomes 8.52 but when the answer…

user2933653
- 97
- 1
- 7
6
votes
2 answers
NSString stringwithformat: Padding 2 strings with unknown length
I'm having a problem doing something with NSString stringWithFormat...
The problem is as follows:
First of all, I have 2 strings. Which are money quantities.
The thing, is that i want to create 1 string with these two.
For ex:
strF = [NSString…

Tidane
- 694
- 1
- 10
- 22
6
votes
2 answers
How to right pad a string using stringWithFormat
I would like to be able to right align a string using spaces. I have to be able to use the stringWithFormat: method.
So far I have tried the recommended format and it does not seem to work: [NSString stringWithFormat:@"%10@",@"test"].
I would…

zachzurn
- 2,161
- 14
- 26
5
votes
3 answers
iphone NSString stringWithFormat and float
I have an input with UIKeyboardTypeDecimalPad and I need my user to input a float (with unlimited characters after a dot). After the input I filter the string with :
NSString *newValue = [NSString stringWithFormat:@"%.f",[textField.text…

Valentin Stoianoff
- 187
- 3
- 3
- 11