tags:

views:

54

answers:

3

hi, 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?

+2  A: 

How about something like:

NSString* str = @"1223.2212311";
NSString* result = [NSString stringWithFormat:@"%.2f", [str floatValue]];
flitzwald
+2  A: 

You should take a look at NSNumberFormatter.

falconcreek
+1 this is the proper way to do this.
Dave DeLong
A: 

If the strings aren't going to be localized, you can just use -[NSString floatValue] to parse them.

Jonathan Grynspan
Anything presented in the UI should be ready for localization. Your customers in France want to see a , symbol for the decimal. If you use NS numberFormatter you get that for free.
falconcreek
This is a question about *parsing* a string into a float, not displaying a float as a string.
Jonathan Grynspan