[英]Printf with Double Formatting Question
I've got the following printf statement: 我有以下printf语句:
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. 但是,val永远不会填充空格,因此如果小数点前有3位或4位数,则val占用的空间不同。 Shouldn't the 4 in the format specifier guarantee that there are at least 4 spaces?
格式说明符中的4不应该保证至少有4个空格吗?
Sorry for the newb question but it's been frustrating me. 对不起,这个问题让我感到很沮丧。
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. 4指定最小总字段 - 您还有小数点和小数点后的2位数,这4个字符的字段宽度中至少包含3个字符。
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
. 因此,如果您希望小数点前的最小字段宽度为4个字符,则需要使用
%-7.2lf
格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.