I would like to print doubles with consistent column width. Theses numbers represent dollar values so I would also like to have no more than two digits after the decimal print.
Here I’m intending to use both the specifier for significant figures (%1.2f
) which has always worked for me in the past, as well as the column width specifier (%-10f
) which is set to align left here. The only problem here is that I have no idea how to combine them in a way where a number will both be truncated after the 10th place as well as be aligned to the left of a ten character column.
void displayBooks(float *balances, float *interests, int size) {
printf("balance interestn");
for (int i=0; i<size; ++i) {
printf("%1.2-10f", balances[i]);
printf("%1.2-10f", interests[i]);
}
}
This wasn’t recognized as a format specifier at all. It just prints %1.2-10f
literally. How might I modify my code to achieve the desired behavior?
5
""%1.2-10f"
is an invalid format string. You probably want to use "%-10.2f"
for left adjusting the field with a minimum field width of 10 and a precision of 2. Below I used ‘*’ to pass in the minimum field width instead of repeating the magic value. Format the header along the same lines.
#include <stdio.h>
#define MIN_FIELD_WIDTH -10
void displayBooks(float *balances, float *interests, int size) {
printf("%*s%*sn",
MIN_FIELD_WIDTH, "balance",
MIN_FIELD_WIDTH, "interest"
);
for (int i=0; i<size; ++i) {
// Consider using a space prefix and MIN_FIELD_WIDTH-1
// to ensure two fields don't run into each other.
printf("%*.2f", MIN_FIELD_WIDTH, balances[i]);
printf("%*.2f", MIN_FIELD_WIDTH, interests[i]);
}
printf("n");
}
int main() {
displayBooks((float []) {123.123}, (float []) {234.234}, 1);
}
and example run:
balance interest
123.12 234.23
5
For printf
‘s floating-point formats like %f
, there are two numbers you can specify. If you say %10f
, you’re asking for a field at least 10 characters wide. If you say %.2f
, you’re asking for a fixed precision of 2 places past the decimal point. And if you say %10.2f
, you’re asking for both at the same time.
You were correct that %1.2f
gave you two places past the decimal, but the 1
there — which means “at least 1 character” — is basically meaningless, since the .2
part already gives you three characters.
You were correct that "%-10f"
gave you a field width 10 characters wide, left aligned — although I suspect you want right alignment here.
So if you combine these two, you’d get "%-10.2f"
. As I said, there are only two numbers you can specify, so the only meaningful route is to have the new width -10
replace the earlier, not-very-useful width of 1
.
But to be clear, that thing you came up with, "%1.2-10f"
, is, I’m sorry, quite meaningless. It doesn’t match the required pattern "%M.Nf"
; there’s nothing printf
can do with it.
But then, as I said, I think you want right adjustment, without the -
modifier. When I change your code to
void displayBooks(float *balances, float *interests, int size) {
printf(" balance interestn");
for (int i=0; i<size; ++i) {
printf("%10.2f", balances[i]);
printf("%10.2fn", interests[i]);
}
}
, I get output like this:
balance interest
123.45 1.23
6.78 4.56
12345.67 7.89
That looks pretty good to me. (Note, however, that it will not “truncate after the 10th place”, as you asked for. printf
never truncates numbers like that.)
One more thing: Type float
probably won’t give you enough precision for your accounting program, if you intend to use it for anything serious. Type float
can represent approximately 7 decimal digits accurately, but that only gives you 5 to the left of the decimal point, for a maximum balance of $99,999.99. Really, in this as in almost all cases, if would be much safer to use type double
everywhere.