Reputation: 8516
I have the following double value: 0.000900137869045636
which I would like to convert to a NSString
.
I've tried the following code, but no matter what, it rounds the value to this: 0.000900138
[NSString stringWithFormat:@"%.20lf", [currentLocation coordinate].latitude];
What should I do to keep the double number as it is?
Thank you.
Upvotes: 21
Views: 33196
Reputation: 53010
Short answer: use %.18lf
- tested with 0.000900137869045633 -> 0.000900137869045639. But don't kid yourself this is precise, it works only due to rounding.
Upvotes: 2
Reputation: 22412
There doesn't seem to be ANY issue with your code as far as I can see: I just tried the following and
int main(int argc, const char * argv[])
{
double foo = 0.000900137869045636;
NSLog([NSString stringWithFormat:@"%.20lf", foo]);
return 0;
}
the log output is:
2013-05-10 14:00:18.492 Test[71101:303]
0.00090013786904563598
So not sure what your issue is, but it's not in the NSString creation.
Upvotes: 0
Reputation: 1209
Create an NSNumber using
NSNumber *myDoubleNumber = [NSNumber numberWithDouble:myDouble];
Then call
[myDoubleNumber stringValue];
Upvotes: 54