Printf 与双重格式问题

发布于 2024-09-14 13:34:30 字数 191 浏览 2 评论 0原文

我有以下 printf 语句:

printf("val=%-4.2lf", val);

但是,val 永远不会用空格填充,因此如果小数点前有 3 或 4 位数字,则 val 占用的空间会有所不同。格式说明符中的 4 不应该保证至少有 4 个空格吗?

很抱歉这个新问题,但这让我很沮丧。

I've got the following printf statement:

printf("val=%-4.2lf", val);

However, val is never padded with spaces so the space taken up by val is different if there are 3 or 4 digits before the decimal. Shouldn't the 4 in the format specifier guarantee that there are at least 4 spaces?

Sorry for the newb question but it's been frustrating me.

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

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

发布评论

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

评论(1

深爱不及久伴 2024-09-21 13:34:30

4 指定最小 total 字段 - 您还可以指定小数点和小数点后的 2 位数字,这些数字在 4 个字符字段宽度中至少占 3 个字符。

因此,如果您希望小数点前的最小字段宽度为 4 个字符,则需要使用 %-7.2lf 格式。

The 4 specifies the minimum total field with - you also have the decimal point and the 2 digits after the decimal that are taking a minimum of 3 characters out of that 4 character field width.

So if you want a minimum field width of 4 characters before the decimal point, you'd want to use a format of %-7.2lf.

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