[英]printf: how to align numbers on decimal point
consider having a set of numbers: 考虑有一组数字:
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);
I would like to have the decimal point aligned at the n-th column of a total width of m (maybe n=6 and m=25) 我想将小数点对齐在总宽度为m的第n列(可能n = 6和m = 25)
If using %f
i get nicely aligned numbers, but the numbers that require an scientific notation are being destroyed. 如果使用%f
我得到了很好的对齐数字,但是需要科学记数法的数字正在被破坏。 usging %g
interprets the precision argument after the dot as an absolute precision that results in different decimals after the decimal point. usging %g
将点后面的precision参数解释为绝对精度,导致小数点后面的小数点不同。 And since the most numbers are in the range (-10, 10) I do not want to take the scientific notation %e
由于大多数数字在(-10,10)范围内,我不想采用科学记数法%e
Are there any flags or format attributes (or combination of) that I overlooked? 是否有我忽略的标志或格式属性(或组合)?
the expected outcome would be: 预期的结果将是:
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 ]
or even better 甚至更好
[ +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 ]
(the question is about Perl but s?printf
s format string is rather independent, so I also added C
tag) (问题是关于Perl但是s?printf
的格式字符串是相当独立的,所以我也添加了C
标签)
The [*]printf
function allow you to: [*]printf
函数允许您:
So if you know how much characters are before the dot ( d = sprintf(buf, "%.0f", ar[i]);
), you can align the dot using ( printf("[%*s %g", 4-d, "", ar[i]);
). 因此,如果您知道点之前有多少字符( d = sprintf(buf, "%.0f", ar[i]);
),您可以使用( printf("[%*s %g", 4-d, "", ar[i]);
)。
Then same logic to align the closing bracket: 然后使用相同的逻辑来对齐右括号:
#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;
}
Gives: 得到:
[ +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 ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.