I have a double number and I would like to convert it to string.
The number is, for example, something like
24.043333332154465777...
but if I convert it to string using something like
NSString *myString = [NSString stringWithFormat:#"%f", myDouble];
The string is just
24.043333
how do I get a full string the corresponds to the whole double number? What other methods do I have to convert this?
Another option, since you asked for other ways in your comment to mipadi's answer:
Create an NSNumber using NSNumber *myDoubleNumber = [NSNumber numberWithDouble:myDouble];
Then call [myDoubleNumber stringValue];
From the docs:
Returns the receiver’s value as a human-readable string, created by invoking descriptionWithLocale: where locale is nil.
[NSString stringWithFormat:#"%.20f", myDouble];
or
#(myDouble).stringValue;
You can pass a width format specifier to stringWithFormat.
NSString *myString = [NSString stringWithFormat:#"%.20f", myDouble];
will format myDouble with 20 decimal places.
There's also NSNumberFormatter.
[NSString stringWithFormat:#"%.0f", myDouble];
Use this for no decimal value
Related
I want to have a percentage sign in my string after a digit. Something like this: 75%.
How can I have this done? I tried:
[NSString stringWithFormat:#"%d\%", someDigit];
But it didn't work for me.
The code for percent sign in NSString format is %%. This is also true for NSLog() and printf() formats.
The escape code for a percent sign is "%%", so your code would look like this
[NSString stringWithFormat:#"%d%%", someDigit];
Also, all the other format specifiers can be found at Conceptual Strings Articles
If that helps in some cases, it is possible to use the unicode character:
NSLog(#"Test percentage \uFF05");
The accepted answer doesn't work for UILocalNotification. For some reason, %%%% (4 percent signs) or the unicode character '\uFF05' only work for this.
So to recap, when formatting your string you may use %%. However, if your string is part of a UILocalNotification, use %%%% or \uFF05.
seems if %% followed with a %#, the NSString will go to some strange codes
try this and this worked for me
NSString *str = [NSString stringWithFormat:#"%#%#%#", #"%%",
[textfield text], #"%%"];
uese following code.
NSString *searchText = #"Bhupi"
NSString *formatedSearchText = [NSString stringWithFormat:#"%%%#%%",searchText];
will output: %Bhupi%
iOS 9.2.1, Xcode 7.2.1, ARC enabled
You can always append the '%' by itself without any other format specifiers in the string you are appending, like so...
int test = 10;
NSString *stringTest = [NSString stringWithFormat:#"%d", test];
stringTest = [stringTest stringByAppendingString:#"%"];
NSLog(#"%#", stringTest);
For iOS7.0+
To expand the answer to other characters that might cause you conflict you may choose to use:
- (NSString *)stringByAddingPercentEncodingWithAllowedCharacters:(NSCharacterSet *)allowedCharacters
Written out step by step it looks like this:
int test = 10;
NSString *stringTest = [NSString stringWithFormat:#"%d", test];
stringTest = [[stringTest stringByAppendingString:#"%"]
stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]];
stringTest = [stringTest stringByRemovingPercentEncoding];
NSLog(#"percent value of test: %#", stringTest);
Or short hand:
NSLog(#"percent value of test: %#", [[[[NSString stringWithFormat:#"%d", test]
stringByAppendingString:#"%"] stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet alphanumericCharacterSet]] stringByRemovingPercentEncoding]);
Thanks to all the original contributors. Hope this helps. Cheers!
I have a couple of floats and they all are stored in a mutable string. But while displaying them it's showing 2011.00000 inspite of 2011 so i want it to format it.
Any suggestions.
Thanks
NSString* str = [NSString stringWithFormat:#"%.f", float_number];
Could you just put them into floats and then use a stringWithFormat(#"%f4.0", ?
Use
NSString* sa = [NSString stringWithFormat: #"%0.0f", 22323.4342];
i am having NSString* str = #"1223.2212311";
i want to convert it as 1223.22(after floating point two chars),is it possible through NSString?i have to use NSScanner? any help please?
How about something like:
NSString* str = #"1223.2212311";
NSString* result = [NSString stringWithFormat:#"%.2f", [str floatValue]];
You should take a look at NSNumberFormatter.
If the strings aren't going to be localized, you can just use -[NSString floatValue] to parse them.
I need to store the memory address of an Object as an NSString. Is there a special format specifier for that?
%p should work for any pointer, including pointers to objects. So:
NSString *addr = [NSString stringWithFormat:#"%p", obj];
That will be a string beginning with 0x and in hexadecimal.
Yes, you can use the %p formatter to get the address of an object as a string. Something like this would work:
NSString *pstr = [NSString stringWithFormat:#"%p", myPtr];
How to connect string "Hello" and string "World" to "HelloWorld"? Looks like "+" doesn't work.
NSString *string = [NSString stringWithFormat:#"%#%#", #"Hello", #"World"];
NSLog(#"%#", string);
That should do the trick, although I am sure there is a better way to do this, just out of memory. I also must say this is untested so forgive me. Best thing is to find the stringWithFormat documentation for NSString.
How about:
NSString *hello = #"Hello";
NSString *world = #"World";
NSString *helloWorld = [hello stringByAppendingString:world];
If you have two literal strings, you can simply code:
NSString * myString = #"Hello" #"World";
This is a useful technique to break up long literal strings within your code.
However, this will not work with string variables, where you'd want to use stringWithFormat: or stringByAppendingString:, as mentioned in the other responses.
there's always NSMutableString..
NSMutableString *myString = [NSMutableString stringWithString:#"Hello"];
[myString appendString: #"World"];
Note:
NSMutableString *myString = #"Hello"; // won't work, literal strings aren't mutable
t3.text=[t1.text stringByAppendingString:t2.text];
Bill, I like yout simple solution and I'd like to note that you can also eliminate the space between the two NSStrings:
NSString * myString = #"Hello"#"World";