简体   繁体   English

QTextStream按点对齐(小数点分隔符)

[英]QTextStream align by dot (decimal separator)

I'm using QTextStream to write data to file. 我正在使用QTextStream将数据写入文件。 It is nicely organized (almost perfect) like: 它组织得很好(几乎完美),例如:

i           t           y          yy           
0   0.0166667   -0.649999     67.6666           
1   0.0333333    0.477777    -43.4444           
2        0.05   -0.246295     30.6295           
3   0.0666666    0.264197     -18.753           
4   0.0833333  -0.0483533     14.1687           
5         0.1    0.187791     -7.7791           
6    0.116667   0.0581394     6.85273           
7    0.133333    0.172351    -2.90181           
8        0.15    0.123988     3.60121           
9    0.166667    0.184008   -0.734136

The above is produced by 以上是由

    stream <<  qSetFieldWidth(5)  << i
           <<  qSetFieldWidth(12) << t
           <<  qSetFieldWidth(12) << y
           <<  qSetFieldWidth(12) << yy
           << endl;

But I would like columns aligned by dot (the decimal separator), like: 但我想按点(小数点分隔符)对齐的列,例如:

0   0.0166667   -0.649999     67.6666           
1   0.0333333    0.477777    -43.4444           
2   0.05        -0.246295     30.6295           
3   0.0666666    0.264197    -18.753           
4   0.0833333   -0.0483533    14.1687           
5   0.1          0.187791     -7.7791           
6   0.116667     0.0581394     6.85273           
7   0.133333     0.172351     -2.90181           
8   0.15         0.123988      3.60121           
9   0.166667     0.184008     -0.734136

How do I do that? 我怎么做?

You'd have to break the number into integral (right-aligned) and fractional parts (left-aligned) (and strip off the leading zero from the fractional representation). 您必须将数字分为整数部分(右对齐)和小数部分(左对齐)(并从小数表示中除去前导零)。

A simpler way would be to pad the output with spaces appropriate to the number of digits in the integer representation (accounting for the sign as well). 一种更简单的方法是在输出中填充与整数表示形式中的位数相对应的空格(也要考虑符号)。

UNTESTED: UNTESTED:

// inefficient, but illustrates the concept:
int NumIntDig(double x) {
    stringstream s;
    s << int(x);
    return s.str().size();
} 

stream <<  qSetFieldAlignment(AlignRight) << qSetFieldWidth(5)  << i
       <<  qSetFieldAlignment(AlignLeft) <<
       <<  qSetFieldWidth(4-NumIntDig(t)) << " "  
       <<  qSetFieldWidth(8+NumIntDig(t)) << t
       <<  qSetFieldWidth(4-NumIntDig(y)) << " "  
       <<  qSetFieldWidth(8+NumIntDig(t)) << y
       <<  qSetFieldWidth(4-NumIntDig(yy)) << " "  
       <<  qSetFieldWidth(8+NumIntDig(t)) << yy
       << endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM