How to combine significant figures, column width, and alignment specifiers?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật