Convert a Cocoa Touch latitude/longitude to a string without degree symbol
-
22-08-2019 - |
Question
I'm trying to convert CLLocation
latitude / longitude to a string. I can successfully do this with the follow code:
// extract latitude from CLLocation object and cast to string
NSString *latitude = [[NSString alloc] initWithFormat:@"%g°", location.coordinate.latitude];
this gives me a value like: 34.10111º. I would like this number as a pure string without the º degree symbol.
Should I init the string with a different format?
I tried initing with the format @"%d" and the string comes out to a different number altogether.
Solution
You have a degree symbol in your format string. Remove it and you should be fine.
As to the other part of your question, %d
as a format specifier wants an integer, and you're giving it a floating-point number. Your %g
is correct, as would be %e
or %f
.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow