将Cocoa Touch纬度/经度转换为不带度数符号的字符串

时间:2022-12-31 16:02:57

I'm trying to convert CLLocation latitude / longitude to a string. I can successfully do this with the follow code:

我正在尝试将CLLocation纬度/经度转换为字符串。我可以使用以下代码成功完成此操作:

// 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.

这给了我一个像34.10111º的值。我希望这个数字是一个没有º度符号的纯字符串。

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.

我尝试使用格式@“%d”启动,字符串完全出现在另一个数字上。

1 个解决方案

#1


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.

至于你问题的另一部分,%d作为格式说明符需要一个整数,你给它一个浮点数。你的%g是正确的,因为%e或%f。

#1


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.

至于你问题的另一部分,%d作为格式说明符需要一个整数,你给它一个浮点数。你的%g是正确的,因为%e或%f。