Formatting a float number inside a Mutable string - iphone

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];

Related

How to combine two strings in Objective-C for an iPhone app

How can I combine "stringURL" and "stringSearch" together?
- (IBAction)search:(id)sender;{
stringURL = #"http://www.websitehere.com/index.php?s=";
stringSearch = search.text;
/* Something such as:
stringURL_ = stringURL + stringSearch */
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:stringURL_]]];
}
Philippe gave a good example.
You can also use plain stringWithFormat: method.
NSString *combined = [NSString stringWithFormat:#"%#%#", stringURL, stringSearch];
This way you can manipulate string even more by putting somethig inbetween the strings like:
NSString *combined = [NSString stringWithFormat:#"%#/someMethod.php?%#", stringURL, stringSearch];
NSString* combinedString = [stringUrl stringByAppendingString: search.text];
NSString * combined = [stringURL stringByAppendingString:stringSearch];
Instead of stringByAppendingString:, you could also use
NSString *combined = [NSString stringWithFormat: #"%#%#",
stringURL, stringSearch];
This is especially interesting/convenient if you have more than one string to append. Otherwise, the stringbyAppendingString: method is probably the better choice.
You can use stringByAppendingString:
stringURL = [#"http://www.websitehere.com/index.php?s="
stringByAppendingString:search.text];
If you want to have some control about the format of the parameter you should assemble
your URL string with
[NSString stringWithFormat:#"http://www.websitehere.com/index.php?s=%#", search.text]
This solution is charming because you can append almost anything which can be inserted into a printf-style format.
I would not have given the answer of such general question.
There are many answers of same type question have already given. First find the answer of your question from existing question.
NSString* myURLString = [NSString stringWithFormat:#"http://www.websitehere.com/index.php?s=%#", search.text];

Remove the first 4 letters from string

Hi I am having a string as G531 Other Dough Products.
Here I want only Other Dough Products, reply me with code.
I tried with substring but that is not working.
Thanks in advance .
You don't actually say what you tried but substringFromIndex:
NSString *str = #"G531 Other Dough Products";
str = [str substringFromIndex:5];
should do the trick.
You'll probably find that you want to use 5 unless you want the leading space in your new string.
NSString* s = [#"G531 Other Dough Products" substringFromIndex:5];
Try using "substringFromIndex" (see documentation)
He He, you can also do this as below,
NSString * newString = [NSString stringWithCString:(const char*)&"G531 Other Dough Products"[5] encoding:NSUTF8StringEncoding];
NSLog(#"%#", newString);

NSString scanning?

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.

cocoa - converting a double to string

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

How to concatenate two strings on iPhone?

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";