【问题标题】:Right aligning digits in c?c中的右对齐数字?
【发布时间】:2015-01-26 00:39:34
【问题描述】:

我有一个如下所示的代码 sn-p -

int main() {
  double firstNumber = 245.3252;
  double secondNumber = 32.4324;
  printf("This is my first number: %.2f", firstNumber);
  printf("And what's gonna be in the front if my second number: %.2f", secondNumber);
  return 0;
}

这样的代码的问题是它会给我一个如下所示的输出 -

This is my first number: 245.32
And what's gonna be in the front if my second number: 32.43

但是我希望输出看起来像这样(预期输出)-

This is my first number:                             245.32
And what's gonna be in the front if my second number: 32.43

我试图使用像 %20s 这样的宽度修饰符,但是如果我的第一个数字是十位数字,那么对齐会再次消失。有人可以告诉我如何让它们看起来完全正确对齐吗?谢谢!

【问题讨论】:

    标签: c text-alignment


    【解决方案1】:

    右对齐实际上是默认设置,您只需要确保正确指定宽度即可。你想要这样的东西:

    printf("This is my first number:                              %8.2f", firstNumber);
    printf("And what's gonna be in the front if my second number: %8.2f", secondNumber);
    

    【讨论】:

      【解决方案2】:

      最简单的方法:在第一个语句的格式字符串中添加必要的空格字符,同时确保您还固定数字使用的字符数。

      int main() {
        double firstNumber = 245.3252;
        double secondNumber = 32.4324;
        printf("This is my first number:                              %6.2f", firstNumber);
        printf("And what's gonna be in the front if my second number: %6.2f", secondNumber);
        return 0;
      }
      

      【讨论】:

        【解决方案3】:

        格式化前导文本

        int main(void) {
          double Number[] = { 245.3252, 32.4324 };
          const char *text[] = {"This is my first number:", 
                                 "And what's gonna be in the front if my second number:"};
        
          // Find or set max width of text
          int width = max(strlen(text[0]), strlen(text[1]));
        
          // Calculate or set max width of numbers
          int maxnumwidth = 6;
        
          int i;
          for (int i= 0; i < 2; i++) {
            printf("%-*s %*.2f\n", width, text[i], maxnumwidth, Number[i]);
          }
        
          return 0;
        }
        

        %-*s:
        -左对齐
        *从参数中获取宽度。
        s字符串

        %*.2f:
        * 从参数中获取宽度。
        .2 打印到小数点后 2 位。
        f 浮点数或双精度数

        【讨论】:

          【解决方案4】:

          您可以插入空格。

          我们将最长字符串的长度称为m 和另一个n。在这个例子中,最长的字符串是“And what's going to be in the front if my second number: 32.43”。

          现在,最长的字符串不需要任何修改。但是,其他字符串将需要在 句后添加m - n 空格,而在之前 数字。所以,在“This is my first number:”之后,但在“245.32”之前。

          注意:在固定宽度字体中,这将是对齐的。但是,在某些字体中,使用此方法无法正常对齐,因为字符的宽度不固定。

          【讨论】:

            【解决方案5】:

            好吧,对于你的特定问题,试试这个:

            printf("这是我的第一个数字:\t\t\t %.2f \n", firstNumber); printf("如果我的第二个数字在前面会是什么:%.2f", secondNumber);

            \t\t\t(3次)+%.2f前4个空格

            【讨论】:

            • 只是我试图帮助用户解决特定问题。我不推荐这个。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-01-19
            • 1970-01-01
            • 2011-10-09
            • 2013-09-30
            • 2011-03-07
            • 2016-12-15
            • 2010-11-26
            相关资源
            最近更新 更多