printf:如何在小数点上对齐数字

时间:2018-12-12 08:33:34

标签: c perl printf

考虑使用一组数字:

my @array =   (
   1.788139343e-007, 0.0547055073198, -0.703213036125,
   -0.583665880391, -1.41198285298, +0.171879081676,
   -0.58966025098, -86.0627173425, -0.84449797709,
 3.49876623321e-005, 3.02660429162, -0.256948695361);

我想将小数点对齐到总宽度为m的第n列(也许n = 6和m = 25)

如果使用%f,我得到的数字会对齐,但是需要科学计数法的数字会被破坏。 使用%g会将点后的精度参数解释为绝对精度,从而导致小数点后的小数点不同。 而且由于大多数数字都在(-10,10)范围内,所以我不想采用科学计数法%e

我是否忽略了任何标志或格式属性(或其组合)?

预期结果将是:

foreach my $f (@array){
  printf("[%+25.12g]$/", $f);
}


[   +1.788139343e-007     ]
[   +0.0547055073198      ]
[   -0.703213036125       ]
[   -0.583665880391       ]
[   -1.41198285298        ]
[   +0.0171879081676      ]
[   -0.58966025098        ]
[  -86.0627173425         ]
[   -0.84449797709        ]
[   +3.49876623321e-005   ]
[   +3.02660429162        ]
[   -0.256948695361       ]

甚至更好

[   +1.7881393430000e-007 ]
[   +0.0547055073198      ]
[   -0.7032130361250      ]
[   -0.5836658803910      ]
[   -1.4119828529800      ]
[   +0.0171879081676      ]
[   -0.5896602509800      ]
[  -86.0627173425000      ]
[   -0.8444979770900      ]
[   +3.4987662332100e-005 ]
[   +3.0266042916200      ]
[   -0.2569486953610      ]

(这个问题与Perl有关,但是s?printf的格式字符串是非常独立的,因此我还添加了C标签)

1 个答案:

答案 0 :(得分:6)

通过[*]printf函数,您可以:

  • 获取已写入的字符数,
  • 添加一些填充。

因此,如果您知道点(d = sprintf(buf, "%.0f", ar[i]);)之前有多少个字符,可以使用(printf("[%*s %g", 4-d, "", ar[i]);)对齐点。

然后使用相同的逻辑对齐右括号:

#include <stdio.h>

int main()
{
    double ar[] = {
        1.788139343e-007,   0.0547055073198, -0.703213036125,
       -0.583665880391,    -1.41198285298,    0.171879081676,
       -0.58966025098,    -86.0627173425,    -0.84449797709,
        3.49876623321e-005, 3.02660429162,   -0.256948695361};

    for (int i = 0; i < 12; ++i)
    {
        /* buffer to count how much character are before the dot*/
        char buf[64];

        /* how much before the dot? */
        int d = sprintf(buf, "%+.0lf", ar[i]);

        /* write float with aligned dot and store second padding */
        int e = printf("[%*s %+.15lg", 4-d, "", ar[i]);
        printf("%*s]\n", 25-e, "");

    }
    return 0;
}

礼物:

[   +1.788139343e-07     ]
[   +0.0547055073198     ]
[   -0.703213036125      ]
[   -0.583665880391      ]
[   -1.41198285298       ]
[   +0.171879081676      ]
[   -0.58966025098       ]
[  -86.0627173425        ]
[   -0.84449797709       ]
[   +3.49876623321e-05   ]
[   +3.02660429162       ]
[   -0.256948695361      ]