c printf 有符号浮点数

发布于 2024-09-27 23:06:11 字数 105 浏览 1 评论 0原文

格式化程序是什么来确保 + 或 - 符号始终显示在 C 中 printf() 中的浮点值前面?
我已经有一段时间没有做过 C 了,所以我在网上哪里可以找到好的参考资料,任何建议都会受到赞赏

What is the formatter to make sure that + or - signs are always shown in front of the float value in printf() in C?
I haven't done C in a while, so where can I find a good reference on the web, any suggestions are appreciated

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

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

发布评论

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

评论(2

那一片橙海, 2024-10-04 23:06:11

% 之后、字段宽度、精度说明符或 f 之前放置一个 + 符号。例如,使用 "%+10.2f" 表示十个字符宽的字段,小数点后有两位数字。

来自 printf(3)

+

符号(+ 或 -)应始终放置在有符号转换生成的数字之前。默认情况下,符号仅用于负数。如果两者都使用,则 + 会覆盖空格。

Put a + sign after the % but before the field width, the precision specifier, or the f. For example, use "%+10.2f" for a ten character wide field with two digits after the decimal.

From printf(3):

+

A sign (+ or -) should always be placed before a number produced by a signed conversion. By default a sign is used only for negative numbers. A + overrides a space if both are used.

当梦初醒 2024-10-04 23:06:11

“%+f”就是您要寻找的内容(尽管请注意,当您打印它时,您实际上会打印一个双精度数,而不是一个浮点数——当传递给一个不支持浮点数的函数时,浮点数会隐式提升为双精度数) t 有原型或可变参数)。

"%+f" is what you're looking for (though note that when you print it, you'll actually be printing a double, not a float -- a float is implicitly promoted to double when passed to a function that doesn't have a prototype or for a variadic argument).

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