gpt4 book ai didi

C printf()格式化: Float doesn't seem to right justify.

转载 作者:太空宇宙 更新时间:2023-11-04 02:38:42 25 4
gpt4 key购买 nike

当我尝试格式化此输出时发生了一些奇怪的事情, float 似乎没有右对齐?

我希望小数点对齐...我做错了什么吗?

if (flushParams -> Volume > 0)
{
printf("\n %-23s %7ld %2s %7ld %3s %2s %4.1f %1s %9s %4.1f %2s %9s %4.1f %2s",
"Intake Flush",
event_data -> water1.ml, "ml",
event_data -> water1.secs, "sec",
"LB", event_data -> water1.bat, "V",
"Average I", event_data -> water1.average_current, "mA",
"Highest I", event_data -> water1.highest_current, "mA");
Message(event_data -> water1.message);
printf("\n %-11s = %02d", "Flush port", event_data -> portwater);
}
printf("\n\n %-23s %7ld %2s %7ld %3s %2s %4.1f %1s %9s %4.1f %2s %9s %4.1f %2s",
"Sample",
event_data -> sample.ml, "ml",
event_data -> sample.secs, "sec",
"LB", event_data -> sample.bat, "V",
"Average I", event_data -> sample.average_current, "mA",
"Highest I", event_data -> sample.highest_current, "mA");
Message(event_data -> sample.message);
printf("\n %-11s = %02d", "Sample port", event_data -> portsample);
printf("\n ");
print_datetime(event_data -> time_off);
printf(" %3.1f Vbat %3.1f %cC PORT = %02d",
event_data -> vbat_off, event_data -> celcius_off, 248, event_data -> portend);

printf("\n");

输出看起来像这样:

  Intake Flush                101 ml       34 sec  LB 22.5 V  Average I 113.0 mA Highest I 118.0 mA Volume reached
Flush port = 00

Sample 320 ml 141 sec LB 22.5 V Average I 91.0 mA Highest I 97.0 mA Volume reached
Sample port = 01
12/09/15 17:11:14 22.6 Vbat 22.5 øC PORT = 01

请注意“平均 I”和“最高 I”值如何不对齐?默认情况下它们不应该右对齐吗?我希望 float 的最后显示数字位于我为 float 指定的 4 个字符空间中最右边的位置。多头显示出我预期的方式。

最佳答案

对于您的数据,将 %4.1f 更改为 %5.1f 将解决此问题。在 %m.nf 格式说明符中,m 是字段的总宽度(以字节为单位),而不是小数点前的位数。

关于C printf()格式化: Float doesn't seem to right justify.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34204787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com