Take the 2-minute tour ×

I want to convert a string into a double and after doing some math on it, convert it back to a string.

How do I do this in Objective-C?

Is there a way to round a double to the nearest integer too?

share|improve this question
Further to Chris Hanson's answer, you can find out more about number formatters, their behaviour, and format strings, from NSNumberFormatter on Mac OS X 10.4 in Data Formatting Programming Guide For Cocoa. –  mmalc Oct 5 '08 at 4:29
Another source of information that might be useful to you is this article about NSNumberFormatter: iphonesdkarticles.com/2008/11/… –  jpm Nov 13 '08 at 15:43
I think the official documentation is pretty good... –  user529758 Aug 21 '13 at 8:10

12 Answers 12

up vote 192 down vote accepted

You can convert an NSString into a double with

double myDouble = [myString doubleValue];

Rounding to the nearest int can then be done as

int myInt = (int)(myDouble + (myDouble>0 ? 0.5 : -0.5))

I'm honestly not sure if there's a more streamlined way to convert back into a string than

NSString* myNewString = [NSString stringWithFormat:@"%d", myInt];
share|improve this answer
You don't necessarily want to do this, because different locales format numbers differently. Some will write "1000" for one thousand, while others will write "1,000" and others "1.000" - which do you get from -[NSString doubleValue]? –  Chris Hanson Oct 4 '08 at 19:06
Double is a numeric data type - it does not contain any formatting. Calling [NSString doubleValue] would return 1000 because it's just a number. –  Andy Oct 5 '08 at 12:53
And an NSString cannot contain a number, only a representation of a number. That representation may be in any of a variety of formats that differ by locale. –  Chris Hanson Oct 6 '08 at 6:08
you might want to integrate @Barry Wark's answer in yours, [[NSNumber numberWithInt:myInt] stringValue] –  Yar Mar 18 '11 at 17:29
What if the string value isn't a valid double string, then 0.0 will be returned by doubleValue. It seems to me that the solution suggesting the use of NSNumberFormatter is better because if the string doesn't represent a valid double string, then nil is returned. –  Kaydell Nov 7 '14 at 22:00

To really convert from a string to a number properly, you need to use an instance of NSNumberFormatter configured for the locale from which you're reading the string.

Different locales will format numbers differently. For example, in some parts of the world, COMMA is used as a decimal separator while in others it is PERIOD — and the thousands separator (when used) is reversed. Except when it's a space. Or not present at all.

It really depends on the provenance of the input. The safest thing to do is configure an NSNumberFormatter for the way your input is formatted and use -[NSFormatter numberFromString:] to get an NSNumber from it. If you want to handle conversion errors, you can use -[NSFormatter getObjectValue:forString:range:error:] instead.

share|improve this answer
Or you can use a localised scanner, for example one created with [NSScanner localizedScannerWithString:], and then [scanner scanDouble:&aDouble] will scan in a double based on the user's current locale. –  dreamlax Jun 7 '09 at 11:17

Adding to olliej's answer, you can convert from an int back to a string with NSNumber's stringValue:

[[NSNumber numberWithInt:myInt] stringValue]

stringValue on an NSNumber invokes descriptionWithLocale:nil, giving you a localized string representation of value. I'm not sure if [NSString stringWithFormat:@"%d",myInt] will give you a properly localized reprsentation of myInt.

share|improve this answer
Thanks Barry. The NSNumber to stringValue line you posted worked for me, whereas the NSString stringWithFormat did not. –  mark Jun 20 '11 at 17:35

olliej's rounding method is wrong for negative numbers

  • 2.4 rounded is 2 (olliej's method gets this right)
  • −2.4 rounded is −2 (olliej's method returns -1)

Here's an alternative


You could of course use a rounding function from math.h

share|improve this answer

Here's a working sample of NSNumberFormatter reading localized number String (xCode 3.2.4, osX 10.6), to save others the hours I've just spent messing around. Beware: while it can handle trailing blanks such as "8,765.4 ", this cannot handle leading white space and this cannot handle stray text characters. (Bad input strings: " 8" and "8q" and "8 q".)

NSString *tempStr = @"8,765.4";  
     // localization allows other thousands separators, also.
NSNumberFormatter * myNumFormatter = [[NSNumberFormatter alloc] init];
[myNumFormatter setLocale:[NSLocale currentLocale]]; // happen by default?
[myNumFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
     // next line is very important!
[myNumFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; // crucial

NSNumber *tempNum = [myNumFormatter numberFromString:tempStr];
NSLog(@"string '%@' gives NSNumber '%@' with intValue '%i'", 
    tempStr, tempNum, [tempNum intValue]);
[myNumFormatter release];  // good citizen
share|improve this answer
What does numberFromString return if the string is not a valid string? –  Ben Clayton Sep 4 '12 at 10:00
numberFromString: returns an NSNumber object. See NumberFormatting and NSNumberFormatter in apple docs. (I'm not sure how NSNumber objects encode "not a number" or "bad number", but I see there is a NSNull possible value.) –  miker Nov 29 '12 at 17:00
// Converting String in to Double

double doubleValue = [yourString doubleValue];

// Converting Double in to String
NSString *yourString = [NSString stringWithFormat:@"%.20f", doubleValue];
// .20f takes the value up to 20 position after decimal

// Converting double to int

int intValue = (int) doubleValue;
int intValue = [yourString intValue];
share|improve this answer

For rounding, you should probably use the C functions defined in math.h.

int roundedX = round(x);

Hold down Option and double click on round in Xcode and it will show you the man page with various functions for rounding different types.

share|improve this answer

For conversion from a number to a string, how about using the new literals syntax (XCode >= 4.4), its a little more compact.

int myInt = (int)round( [@"1.6" floatValue] );

NSString* myString = [@(myInt) description];

(Boxes it up as a NSNumber and converts to a string using the NSObjects' description method)

share|improve this answer

from this example here, you can see the the conversions both ways:

NSString *str=@"5678901234567890";

long long verylong;
NSRange range;
range.length = 15;
range.location = 0;

[[NSScanner scannerWithString:[str substringWithRange:range]] scanLongLong:&verylong];

NSLog(@"long long value %lld",verylong);
share|improve this answer

I ended up using this handy macro:

#define STRING(value)               [@(value) stringValue]
share|improve this answer

This is the easiest way I know of:

float myFloat = 5.3;
NSInteger myInt = (NSInteger)myFloat;
share|improve this answer

convert text entered in textfield to integer

double mydouble=[_myTextfield.text doubleValue];

rounding to the nearest double


rounding to the nearest int(considering only positive values)

int myint=(int)(mydouble);

converting from double to string

myLabel.text=[NSString stringWithFormat:@"%f",mydouble];


NSString *mystring=[NSString stringWithFormat:@"%f",mydouble];

converting from int to string

myLabel.text=[NSString stringWithFormat:@"%d",myint];


share|improve this answer

Your Answer


