如何使用变量代替常量

发布于 2024-09-17 11:22:50 字数 208 浏览 4 评论 0原文

如何使用变量 i 代替 '1.4'。我想使用变量 i 来缩小和放大 Web 视图。

[tp stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = '1.4';"];

提前致谢

how to use variable i in place of '1.4'.i want to use variable i to zoom out and zoom in the webview.

[tp stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = '1.4';"];

Thanks in advance

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

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

发布评论

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

评论(4

终止放荡 2024-09-24 11:22:50

您可以使用 NSString 类方法 stringWithFormat:

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%f';", i];

You can use the NSString class method stringWithFormat:

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%f';", i];
鸵鸟症 2024-09-24 11:22:50

您可以使用stringWithFormat:创建字符串。

类似于 [tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%d'", i];

You can create a string using stringWithFormat:.

Something like [tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%d'", i];

岁月蹉跎了容颜 2024-09-24 11:22:50

要使用 stringWithFormat 创建可传递给 stringByEvaluatingJavaScriptFromString 的字符串,您需要知道标量变量 i 的类型。

如果是 int,则使用“%d”。
如果是浮点数,则使用“%f”。
如果是双重使用'%lf'。

或者你可以先将其转换为某种类型:

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%lf';", (double)i];

To use stringWithFormat to create a string that you can pass to stringByEvaluatingJavaScriptFromString, you need to know the type of your scalar variable i.

If it's an int, use '%d'.
If it's a float, use '%f'.
If it's a double use '%lf'.

Or you could cast it to a certain type first:

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%lf';", (double)i];
捂风挽笑 2024-09-24 11:22:50

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%f';", i]

%f 存储 i 并表示它是一个浮点数。

附带说明一下,如果您想指定显示多少位小数,您可以写
%.02f

零告诉它用零填充数字。 “2”表示显示 2 位小数。
另一个例子:
如果需要三位小数,请使用 %.03f
如果您不需要零填充,请删除 0。

对于 1.4,您将使用 %.1f

[tp stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.body.style.zoom = '%f';", i]

%f stores i and says it is a float.

On a side note, if you want to specify how many decimals are displayed you would write
%.02f

The zero tells it to pad the number with zeros. The '2' says show 2 decimal places.
Another example:
If you want three decimal places, use %.03f
If you don't want the padding of zeros, remove the 0.

For 1.4, you would use %.1f

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文