对齐printf()的变量和小数在C中 [英] Aligning printf() variables and decimals in C

查看:317
本文介绍了对齐printf()的变量和小数在C中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大问题,今天℃。所以我想我的变量对齐列,并在同一时间2位小数。

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

我知道去我需要使用%.2f 2位小数,如果我想的宽度我用%-30s。但是我不能将它们合并。见我的code以下,你就明白了。

I know to get to 2 decimal places I need to use %.2f and if I want a width I use %-30s. But I can't combine them. See my code below and you will understand.

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

乎乎,totalhuhu,哈哈,totalhaha都是浮点数。我想在项目的项目,价格下价格一致,总在总量完全一致共1是否比其他更多的数字。

huhu, totalhuhu, haha, totalhaha are all float numbers. I want item under item, price aligned under price and the totals under the total well aligned whether 1 total has more digits than the other.

感谢的人。

推荐答案

就个人而言,我会避免标签输出。你可以得到比对工作,如果你细心 - 主要是通过使用相同的格式字符串的每个项目。 (您所选择的变量名,使这难以完全自动化;也有优势,结构数组)

Personally, I'd avoid tabs in the output. You can get the alignment to work if you're careful — primarily by using the same format string for each item. (Your choice of variable names makes that harder to fully automate; there are advantages to arrays of structures.)

如果您想货币敏感的格式,看看 的strfmon() 功能,并记住,一个C程序中的C语言环境运行,直到您使用设置了不同的语言环境 的setlocale()

If you want currency-sensitive formatting, look at the strfmon() function, and remember that a C program runs in the C locale until you set a different locale using setlocale().

#include <stdio.h>
#include <locale.h>
#include <monetary.h>

int main(void)
{
    double huhu = 123.45;
    double haha = 234.56;
    double huhu_price = 3.34;
    double haha_price = 44.50;
    double totalhuhu = huhu * huhu_price;
    double totalhaha = haha * haha_price;
    char *huhu_name = "Item A";
    char *haha_name = "Much Longer Name";

    setlocale(LC_ALL, "");

    const char fmt[] = "%-30s %10.2f @ $%6.2f $%20.2f\n";
    printf(fmt, huhu_name, huhu, huhu_price, totalhuhu);
    printf(fmt, haha_name, haha, haha_price, totalhaha);

    char buffer1[32];
    char buffer2[32];
    const char p_fmt[] = "%-30s %10.2f @ %s %s\n";
    const char price[] = "%(7.2n";
    const char cost[]  = "%(21.2n";
    strfmon(buffer1, sizeof(buffer1), price, huhu_price);
    strfmon(buffer2, sizeof(buffer2), cost, totalhuhu);
    printf(p_fmt, huhu_name, huhu, buffer1, buffer2);
    strfmon(buffer1, sizeof(buffer1), price, haha_price);
    strfmon(buffer2, sizeof(buffer2), cost, totalhaha);
    printf(p_fmt, haha_name, haha, buffer1, buffer2);

    return 0;
}

示例输出:

Item A                             123.45 @ $  3.34 $              412.32
Much Longer Name                   234.56 @ $ 44.50 $            10437.92
Item A                             123.45 @   $3.34               $412.32
Much Longer Name                   234.56 @  $44.50            $10,437.92

这篇关于对齐printf()的变量和小数在C中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆