简体   繁体   中英

Printf with Double Formatting Question

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM