Aligning printf() variables and decimals in C

后端 未结 2 562
北海茫月
北海茫月 2021-01-27 02:33

big problem with C today. So I want my variables to align in columns and be 2 decimal places at the same time.

I know to get to 2 decimal places I need to use %.2f and i

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-27 02:50

    Try this:

        printf("ItemA\t\t%30.2f @ $3.34 \t\t$ %30.2f\n", huhu, totalhuhu);
        printf("ItemB\t\t%30.2f @ $44.50\t\t$ %30.2f\n", haha, totalhaha);
    

    The \t are tabs to keep similar spacing. You can add as many as you want until you like it and %30.2f will give you a width of 30 before the decimal and a width of 2 after. This should give you your desired results for spacing.

提交回复
热议问题