如何在字符串中包含百分比?

发布于 2024-12-17 13:09:07 字数 554 浏览 2 评论 0原文

可能的重复:
NSString stringWithFormat 添加百分比

这里是一个菜鸟问题。

我有一个代码:

 int value = [sender value];
 [overLabel setText:[NSString stringWithFormat:@"Overhead: %d", (int)value]];

这将标签设置为(例如):

Overhead: 10

我希望能够将其设置为:

Overhead: 10%

如何将 % 实现到字符串中,这样它就不会被用于打印值,而是被打印作为实际的 % 字符。

谢谢你!

Possible Duplicate:
NSString stringWithFormat adding a percent

a noob question here.

I have a code:

 int value = [sender value];
 [overLabel setText:[NSString stringWithFormat:@"Overhead: %d", (int)value]];

and this sets the label as (for example):

Overhead: 10

I'd like to be able to set it as:

Overhead: 10%

How do I implement the % into the string so it's not being used to print a value, but it's printed as an actual % character.

thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-12-24 13:09:07

只需输入两个%,如下所示:

 [overLabel setText:[NSString stringWithFormat:@"Overhead: %d %%", (int)value]];

Just put two %, like this:

 [overLabel setText:[NSString stringWithFormat:@"Overhead: %d %%", (int)value]];
蓝戈者 2024-12-24 13:09:07

您可以使用两个 % 转义 %

int value = [sender value];
[overLabel setText:[NSString stringWithFormat:@"Overhead: %d%%", (int)value]];

You escape the % using two %.

int value = [sender value];
[overLabel setText:[NSString stringWithFormat:@"Overhead: %d%%", (int)value]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文